2023-02-09 00:25:01 +08:00
|
|
|
|
/*
|
2023-02-05 16:25:42 +08:00
|
|
|
|
* File: list.rs
|
2023-01-29 00:12:16 +08:00
|
|
|
|
* Created Time: 2023-01-18
|
2024-01-02 01:25:37 +08:00
|
|
|
|
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
|
2023-01-29 00:12:16 +08:00
|
|
|
|
*/
|
2024-10-23 18:49:47 +08:00
|
|
|
|
use hello_algo_rust::include::print_util;
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
/* Driver Code */
|
2023-07-26 11:00:53 +08:00
|
|
|
|
fn main() {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 初始化列表
|
2024-03-16 02:13:41 +08:00
|
|
|
|
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
2023-10-09 18:20:42 +08:00
|
|
|
|
print!("列表 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 访问元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
let num = nums[1];
|
2023-09-02 23:54:38 +08:00
|
|
|
|
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 更新元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums[1] = 0;
|
|
|
|
|
print!("将索引 1 处的元素更新为 0 ,得到 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 清空列表
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.clear();
|
|
|
|
|
print!("\n清空列表后 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 在尾部添加元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.push(1);
|
|
|
|
|
nums.push(3);
|
|
|
|
|
nums.push(2);
|
|
|
|
|
nums.push(5);
|
|
|
|
|
nums.push(4);
|
|
|
|
|
print!("\n添加元素后 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 在中间插入元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.insert(3, 6);
|
|
|
|
|
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 删除元素
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.remove(3);
|
|
|
|
|
print!("\n删除索引 3 处的元素,得到 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 通过索引遍历列表
|
2023-09-02 23:54:38 +08:00
|
|
|
|
let mut _count = 0;
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for i in 0..nums.len() {
|
|
|
|
|
_count += nums[i];
|
2023-09-02 23:54:38 +08:00
|
|
|
|
}
|
|
|
|
|
// 直接遍历列表元素
|
|
|
|
|
_count = 0;
|
2023-11-14 21:27:35 +08:00
|
|
|
|
for x in &nums {
|
|
|
|
|
_count += x;
|
|
|
|
|
}
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 拼接两个列表
|
2024-03-16 02:13:41 +08:00
|
|
|
|
let mut nums1 = vec![6, 8, 7, 10, 9];
|
|
|
|
|
nums.append(&mut nums1); // append(移动) 之后 nums1 为空!
|
|
|
|
|
|
|
|
|
|
// nums.extend(&nums1); // extend(借用) nums1 能继续使用
|
2023-10-09 18:20:42 +08:00
|
|
|
|
print!("\n将列表 nums1 拼接到 nums 之后,得到 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 排序列表
|
2023-10-09 18:20:42 +08:00
|
|
|
|
nums.sort();
|
|
|
|
|
print!("\n排序列表后 nums = ");
|
|
|
|
|
print_util::print_array(&nums);
|
2023-07-26 11:00:53 +08:00
|
|
|
|
}
|