2023-02-09 00:25:01 +08:00
|
|
|
|
/*
|
2023-01-29 00:12:16 +08:00
|
|
|
|
* File: array.rs
|
|
|
|
|
* Created Time: 2023-01-15
|
2024-01-02 01:25:37 +08:00
|
|
|
|
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
|
2023-02-09 00:25:01 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
include!("../include/include.rs");
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-06 01:14:03 +08:00
|
|
|
|
use rand::Rng;
|
|
|
|
|
|
2023-08-17 05:13:19 +08:00
|
|
|
|
/* 随机访问元素 */
|
2023-01-29 00:12:16 +08:00
|
|
|
|
fn random_access(nums: &[i32]) -> i32 {
|
|
|
|
|
// 在区间 [0, nums.len()) 中随机抽取一个数字
|
2023-02-06 01:14:03 +08:00
|
|
|
|
let random_index = rand::thread_rng().gen_range(0..nums.len());
|
2023-01-29 00:12:16 +08:00
|
|
|
|
// 获取并返回随机元素
|
|
|
|
|
let random_num = nums[random_index];
|
|
|
|
|
random_num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 扩展数组长度 */
|
2024-05-15 18:31:48 +08:00
|
|
|
|
fn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {
|
2023-02-05 16:25:42 +08:00
|
|
|
|
// 初始化一个扩展长度后的数组
|
2023-01-29 00:12:16 +08:00
|
|
|
|
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
|
|
|
|
|
// 将原数组中的所有元素复制到新
|
|
|
|
|
for i in 0..nums.len() {
|
|
|
|
|
res[i] = nums[i];
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2024-05-15 18:31:48 +08:00
|
|
|
|
fn insert(nums: &mut [i32], num: i32, index: usize) {
|
2023-01-29 00:12:16 +08:00
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
|
|
|
|
for i in (index + 1..nums.len()).rev() {
|
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
}
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 将 num 赋给 index 处的元素
|
2023-01-29 00:12:16 +08:00
|
|
|
|
nums[index] = num;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
/* 删除索引 index 处的元素 */
|
2024-05-15 18:31:48 +08:00
|
|
|
|
fn remove(nums: &mut [i32], index: usize) {
|
2023-01-29 00:12:16 +08:00
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for i in index..nums.len() - 1 {
|
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
fn traverse(nums: &[i32]) {
|
2023-02-09 00:25:01 +08:00
|
|
|
|
let mut _count = 0;
|
2023-01-29 00:12:16 +08:00
|
|
|
|
// 通过索引遍历数组
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for i in 0..nums.len() {
|
|
|
|
|
_count += nums[i];
|
2023-01-29 00:12:16 +08:00
|
|
|
|
}
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 直接遍历数组元素
|
|
|
|
|
for num in nums {
|
|
|
|
|
_count += num;
|
2023-01-29 00:12:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
|
fn find(nums: &[i32], target: i32) -> Option<usize> {
|
|
|
|
|
for i in 0..nums.len() {
|
|
|
|
|
if nums[i] == target {
|
|
|
|
|
return Some(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
fn main() {
|
2023-07-24 22:27:26 +08:00
|
|
|
|
/* 初始化数组 */
|
2024-05-15 18:31:48 +08:00
|
|
|
|
let arr: [i32; 5] = [0; 5];
|
|
|
|
|
let slice: &[i32] = &[0; 5];
|
2023-02-05 16:25:42 +08:00
|
|
|
|
print!("数组 arr = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
|
print_util::print_array(&arr);
|
2024-05-15 18:31:48 +08:00
|
|
|
|
// 在 Rust 中,指定长度时([i32; 5])为数组,不指定长度时(&[i32])为切片
|
2023-01-29 00:12:16 +08:00
|
|
|
|
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
|
2024-05-15 18:31:48 +08:00
|
|
|
|
// Vector 是 Rust 一般情况下用作动态数组的类型
|
|
|
|
|
// 为了方便实现扩容 extend() 方法,以下将 vector 看作数组(array)
|
|
|
|
|
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
2023-02-05 16:25:42 +08:00
|
|
|
|
print!("\n数组 nums = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
|
print_util::print_array(&nums);
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 随机访问
|
2023-01-29 00:12:16 +08:00
|
|
|
|
let random_num = random_access(&nums);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
println!("\n在 nums 中获取随机元素 {}", random_num);
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 长度扩展
|
2024-05-15 18:31:48 +08:00
|
|
|
|
let mut nums: Vec<i32> = extend(&nums, 3);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
print!("将数组长度扩展至 8 ,得到 nums = ");
|
2024-03-18 02:44:03 +08:00
|
|
|
|
print_util::print_array(&nums);
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 插入元素
|
2023-01-29 00:12:16 +08:00
|
|
|
|
insert(&mut nums, 6, 3);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
|
print_util::print_array(&nums);
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 删除元素
|
2023-01-29 00:12:16 +08:00
|
|
|
|
remove(&mut nums, 2);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
print!("\n删除索引 2 处的元素,得到 nums = ");
|
2023-02-09 00:25:01 +08:00
|
|
|
|
print_util::print_array(&nums);
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 遍历数组
|
2023-01-29 00:12:16 +08:00
|
|
|
|
traverse(&nums);
|
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 查找元素
|
2023-02-05 16:25:42 +08:00
|
|
|
|
let index = find(&nums, 3).unwrap();
|
|
|
|
|
println!("\n在 nums 中查找元素 3 ,得到索引 = {}", index);
|
2023-02-09 00:25:01 +08:00
|
|
|
|
}
|