Skip to content

Commit

Permalink
add Rust code array, list (krahets#294)
Browse files Browse the repository at this point in the history
* ✨ feat(codes/rust/array_and_linkedlist): add array

* 🐳 chore(codes/rust): update Cargo.toml

* ✨ feat(codes/rust/array_and_linkedlist): add list

* 📃 docs(codes/rust/array_and_linkedlist): add miss comment

---------

Co-authored-by: xblakicex <[email protected]>
  • Loading branch information
xBLACKICEx and xblakicex authored Jan 28, 2023
1 parent da405b5 commit 80e9651
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
7 changes: 7 additions & 0 deletions codes/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions codes/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
[workspace]
members = [
"chapter_computational_complexity",
"chapter_array_and_linkedlist"
]
16 changes: 16 additions & 0 deletions codes/rust/chapter_array_and_linkedlist/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "chapter_array_and_linkedlist"
version = "0.1.0"
edition = "2021"


[[bin]]
name = "array"
path = "array.rs"

[[bin]]
name = "list"
path = "list.rs"

[dependencies]
rand = "0.8.5"
102 changes: 102 additions & 0 deletions codes/rust/chapter_array_and_linkedlist/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* File: array.rs
* Created Time: 2023-01-15
* Author: xBLACICEx ([email protected])
*/

/* 随机返回一个数组元素 */
fn random_access(nums: &[i32]) -> i32 {
// 在区间 [0, nums.len()) 中随机抽取一个数字
let random_index = rand::random::<usize>() % nums.len();
// 获取并返回随机元素
let random_num = nums[random_index];
random_num
}

/* 扩展数组长度 */
fn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {
// 创建一个长度为 nums.len() + enlarge 的新 Vec
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
// 将原数组中的所有元素复制到新
for i in 0..nums.len() {
res[i] = nums[i];
}
// 返回扩展后的新数组
res
}

/* 在数组的索引 index 处插入元素 num */
fn insert(nums: &mut Vec<i32>, num: i32, index: usize) {
// 把索引 index 以及之后的所有元素向后移动一位
for i in (index + 1..nums.len()).rev() {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
}

/* 删除索引 index 处元素 */
fn remove(nums: &mut Vec<i32>, index: usize) {
// 把索引 index 之后的所有元素向前移动一位
for i in index..nums.len() - 1 {
nums[i] = nums[i + 1];
}
}

#[allow(unused_variables)]
/* 遍历数组 */
fn traverse(nums: &[i32]) {
let mut count = 0;
// 通过索引遍历数组
for _ in 0..nums.len() {
count += 1;
}
// 直接遍历数组
for _ in nums {
count += 1;
}
}

/* 在数组中查找指定元素 */
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() {
let arr = [0; 5];
println!("数组 arr = {:?}", arr);
// 在 Rust 中,指定长度时([i32; 5])为数组
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
// 为了方便实现扩容 extend() 方法,以下将(Vec) 看作数组(Array)也是rust一般情况下使用动态数组的类型
let nums = vec![1, 3, 2, 5, 4];
println!("数组 nums = {:?}", nums);

/* 随机访问 */
let random_num = random_access(&nums);
println!("在 nums 中获取随机元素 {}", random_num);

/* 长度扩展 */
let mut nums = extend(nums, 3);
println!("将数组长度扩展至 8 ,得到 nums = {:?}", nums);

/* 插入元素 */
insert(&mut nums, 6, 3);
println!("在索引 3 处插入数字 6 ,得到 nums = {:?}", nums);

/* 删除元素 */
remove(&mut nums, 2);
println!("删除索引 2 处的元素,得到 nums = {:?}", nums);

/* 遍历数组 */
traverse(&nums);

/* 查找元素 */
let index = find(&nums, 3);
println!("在 nums 中查找元素 3 ,得到索引 = {:?}", index);
}
67 changes: 67 additions & 0 deletions codes/rust/chapter_array_and_linkedlist/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* File: array.rs
* Created Time: 2023-01-18
* Author: xBLACICEx ([email protected])
*/

#[allow(unused_variables)]

/* Driver Code */
fn main() {
/* 初始化列表 */
let mut list: Vec<i32> = vec![1, 3, 2, 5, 4];
println!("列表 list = {:?}", list);

/* 访问元素 */
let num = list[1];
println!("访问索引 1 处的元素,得到 num = {num}");

/* 更新元素 */
list[1] = 0;
println!("将索引 1 处的元素更新为 0 ,得到 list = {:?}", list);

/* 清空列表 */
list.clear();
println!("清空列表后 list = {:?}", list);

/* 尾部添加元素 */
list.push(1);
list.push(3);
list.push(2);
list.push(5);
list.push(4);
println!("添加元素后 list = {:?}", list);

/* 中间插入元素 */
list.insert(3, 6);
println!("在索引 3 处插入数字 6 ,得到 list = {:?}", list);

/* 删除元素 */
list.remove(3);
println!("删除索引 3 处的元素,得到 list = {:?}", list);

/* 通过索引遍历列表 */
let mut count = 0;
for _ in 0..list.len() {
count += 1;
}

/* 直接遍历列表元素 */
count = 0;
for _ in &list {
count += 1;
} // 或者
// list.iter().for_each(|_| count += 1);
// let count = list.iter().fold(0, |count, _| count + 1);

/* 拼接两个列表 */
let mut list1 = vec![6, 8, 7, 10, 9];
list.append(&mut list1); // append(移动) 之后 list1 为空!
// list.extend(&list1); // extend(借用) list1 能继续使用

println!("将列表 list1 拼接到 list 之后,得到 list = {:?}", list);

/* 排序列表 */
list.sort();
println!("排序列表后 list = {:?}", list);
}

0 comments on commit 80e9651

Please sign in to comment.