From 80e9651fc20ef8ecf8a6d26d7e93cf1914d9c1d4 Mon Sep 17 00:00:00 2001 From: xBLACKICEx Date: Sat, 28 Jan 2023 17:12:16 +0100 Subject: [PATCH] add Rust code array, list (#294) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ 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 --- codes/rust/Cargo.lock | 7 ++ codes/rust/Cargo.toml | 1 + .../chapter_array_and_linkedlist/Cargo.toml | 16 +++ .../chapter_array_and_linkedlist/array.rs | 102 ++++++++++++++++++ .../rust/chapter_array_and_linkedlist/list.rs | 67 ++++++++++++ 5 files changed, 193 insertions(+) create mode 100644 codes/rust/chapter_array_and_linkedlist/Cargo.toml create mode 100644 codes/rust/chapter_array_and_linkedlist/array.rs create mode 100644 codes/rust/chapter_array_and_linkedlist/list.rs diff --git a/codes/rust/Cargo.lock b/codes/rust/Cargo.lock index ae2b69a86..6de3eabd1 100644 --- a/codes/rust/Cargo.lock +++ b/codes/rust/Cargo.lock @@ -8,6 +8,13 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chapter_array_and_linkedlist" +version = "0.1.0" +dependencies = [ + "rand", +] + [[package]] name = "chapter_computational_complexity" version = "0.1.0" diff --git a/codes/rust/Cargo.toml b/codes/rust/Cargo.toml index 91b54bc3b..c2e0db42b 100644 --- a/codes/rust/Cargo.toml +++ b/codes/rust/Cargo.toml @@ -2,4 +2,5 @@ [workspace] members = [ "chapter_computational_complexity", + "chapter_array_and_linkedlist" ] \ No newline at end of file diff --git a/codes/rust/chapter_array_and_linkedlist/Cargo.toml b/codes/rust/chapter_array_and_linkedlist/Cargo.toml new file mode 100644 index 000000000..a83a67b0e --- /dev/null +++ b/codes/rust/chapter_array_and_linkedlist/Cargo.toml @@ -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" diff --git a/codes/rust/chapter_array_and_linkedlist/array.rs b/codes/rust/chapter_array_and_linkedlist/array.rs new file mode 100644 index 000000000..e23c6a4f8 --- /dev/null +++ b/codes/rust/chapter_array_and_linkedlist/array.rs @@ -0,0 +1,102 @@ +/** + * File: array.rs + * Created Time: 2023-01-15 + * Author: xBLACICEx (xBLACKICEx@outlook.com) +*/ + +/* 随机返回一个数组元素 */ +fn random_access(nums: &[i32]) -> i32 { + // 在区间 [0, nums.len()) 中随机抽取一个数字 + let random_index = rand::random::() % nums.len(); + // 获取并返回随机元素 + let random_num = nums[random_index]; + random_num +} + +/* 扩展数组长度 */ +fn extend(nums: Vec, enlarge: usize) -> Vec { + // 创建一个长度为 nums.len() + enlarge 的新 Vec + let mut res: Vec = vec![0; nums.len() + enlarge]; + // 将原数组中的所有元素复制到新 + for i in 0..nums.len() { + res[i] = nums[i]; + } + // 返回扩展后的新数组 + res +} + +/* 在数组的索引 index 处插入元素 num */ +fn insert(nums: &mut Vec, 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, 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 { + 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); +} \ No newline at end of file diff --git a/codes/rust/chapter_array_and_linkedlist/list.rs b/codes/rust/chapter_array_and_linkedlist/list.rs new file mode 100644 index 000000000..b9b3d1ee2 --- /dev/null +++ b/codes/rust/chapter_array_and_linkedlist/list.rs @@ -0,0 +1,67 @@ +/** + * File: array.rs + * Created Time: 2023-01-18 + * Author: xBLACICEx (xBLACKICEx@outlook.com) + */ + +#[allow(unused_variables)] + +/* Driver Code */ +fn main() { + /* 初始化列表 */ + let mut list: Vec = 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); +} \ No newline at end of file