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
|
2023-02-05 16:25:42 +08:00
|
|
|
|
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
2023-01-29 00:12:16 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
include!("../include/include.rs");
|
2023-01-29 00:12:16 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
/* Driver Code */
|
2023-02-05 16:25:42 +08:00
|
|
|
|
fn main() {
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 初始化列表
|
|
|
|
|
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
|
|
|
|
|
print!("列表 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 访问元素
|
|
|
|
|
let num = list[1];
|
|
|
|
|
println!("\n访问索引 1 处的元素,得到 num = {num}");
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 更新元素
|
|
|
|
|
list[1] = 0;
|
|
|
|
|
print!("将索引 1 处的元素更新为 0 ,得到 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 清空列表
|
|
|
|
|
list.clear();
|
|
|
|
|
print!("\n清空列表后 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 尾部添加元素
|
|
|
|
|
list.push(1);
|
|
|
|
|
list.push(3);
|
|
|
|
|
list.push(2);
|
|
|
|
|
list.push(5);
|
|
|
|
|
list.push(4);
|
|
|
|
|
print!("\n添加元素后 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 中间插入元素
|
|
|
|
|
list.insert(3, 6);
|
|
|
|
|
print!("\n在索引 3 处插入数字 6 ,得到 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 删除元素
|
|
|
|
|
list.remove(3);
|
|
|
|
|
print!("\n删除索引 3 处的元素,得到 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 通过索引遍历列表
|
|
|
|
|
let mut _count = 0;
|
|
|
|
|
for _ in 0..list.len() {
|
|
|
|
|
_count += 1;
|
|
|
|
|
}
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-07-24 22:27:26 +08:00
|
|
|
|
// 直接遍历列表元素
|
2023-02-09 00:25:01 +08:00
|
|
|
|
_count = 0;
|
2023-07-24 22:27:26 +08:00
|
|
|
|
for _n in &list {
|
2023-02-09 00:25:01 +08:00
|
|
|
|
_count += 1;
|
2023-07-24 22:27:26 +08:00
|
|
|
|
}
|
|
|
|
|
// 或者
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// list.iter().for_each(|_| _count += 1);
|
|
|
|
|
// let _count = list.iter().fold(0, |_count, _| _count + 1);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 拼接两个列表
|
|
|
|
|
let mut list1 = vec![ 6, 8, 7, 10, 9 ];
|
|
|
|
|
list.append(&mut list1); // append(移动) 之后 list1 为空!
|
|
|
|
|
// list.extend(&list1); // extend(借用) list1 能继续使用
|
|
|
|
|
print!("\n将列表 list1 拼接到 list 之后,得到 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
|
2023-02-09 00:25:01 +08:00
|
|
|
|
// 排序列表
|
|
|
|
|
list.sort();
|
|
|
|
|
print!("\n排序列表后 list = ");
|
|
|
|
|
print_util::print_array(&list);
|
2023-02-05 16:25:42 +08:00
|
|
|
|
}
|