-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release Rust code to documents. (#656)
- Loading branch information
Showing
61 changed files
with
1,155 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,72 +4,72 @@ | |
* Author: xBLACICEx ([email protected]), sjinzh ([email protected]) | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
include!("../include/include.rs"); | ||
|
||
/* Driver Code */ | ||
fn main() { | ||
// 初始化列表 | ||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ]; | ||
print!("列表 list = "); | ||
print_util::print_array(&list); | ||
// 访问元素 | ||
let num = list[1]; | ||
println!("\n访问索引 1 处的元素,得到 num = {num}"); | ||
// 更新元素 | ||
list[1] = 0; | ||
print!("将索引 1 处的元素更新为 0 ,得到 list = "); | ||
print_util::print_array(&list); | ||
// 清空列表 | ||
list.clear(); | ||
print!("\n清空列表后 list = "); | ||
print_util::print_array(&list); | ||
// 尾部添加元素 | ||
list.push(1); | ||
list.push(3); | ||
list.push(2); | ||
list.push(5); | ||
list.push(4); | ||
print!("\n添加元素后 list = "); | ||
print_util::print_array(&list); | ||
// 中间插入元素 | ||
list.insert(3, 6); | ||
print!("\n在索引 3 处插入数字 6 ,得到 list = "); | ||
print_util::print_array(&list); | ||
// 删除元素 | ||
list.remove(3); | ||
print!("\n删除索引 3 处的元素,得到 list = "); | ||
print_util::print_array(&list); | ||
// 通过索引遍历列表 | ||
let mut _count = 0; | ||
for _ in 0..list.len() { | ||
_count += 1; | ||
} | ||
// 直接遍历列表元素 | ||
_count = 0; | ||
for _n 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 能继续使用 | ||
print!("\n将列表 list1 拼接到 list 之后,得到 list = "); | ||
print_util::print_array(&list); | ||
// 排序列表 | ||
list.sort(); | ||
print!("\n排序列表后 list = "); | ||
print_util::print_array(&list); | ||
} | ||
fn main() { | ||
// 初始化列表 | ||
let mut list: Vec<i32> = vec![ 1, 3, 2, 5, 4 ]; | ||
print!("列表 list = "); | ||
print_util::print_array(&list); | ||
|
||
// 访问元素 | ||
let num = list[1]; | ||
println!("\n访问索引 1 处的元素,得到 num = {num}"); | ||
|
||
// 更新元素 | ||
list[1] = 0; | ||
print!("将索引 1 处的元素更新为 0 ,得到 list = "); | ||
print_util::print_array(&list); | ||
|
||
// 清空列表 | ||
list.clear(); | ||
print!("\n清空列表后 list = "); | ||
print_util::print_array(&list); | ||
|
||
// 尾部添加元素 | ||
list.push(1); | ||
list.push(3); | ||
list.push(2); | ||
list.push(5); | ||
list.push(4); | ||
print!("\n添加元素后 list = "); | ||
print_util::print_array(&list); | ||
|
||
// 中间插入元素 | ||
list.insert(3, 6); | ||
print!("\n在索引 3 处插入数字 6 ,得到 list = "); | ||
print_util::print_array(&list); | ||
|
||
// 删除元素 | ||
list.remove(3); | ||
print!("\n删除索引 3 处的元素,得到 list = "); | ||
print_util::print_array(&list); | ||
|
||
// 通过索引遍历列表 | ||
let mut _count = 0; | ||
for _ in 0..list.len() { | ||
_count += 1; | ||
} | ||
|
||
// 直接遍历列表元素 | ||
_count = 0; | ||
for _n 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 能继续使用 | ||
print!("\n将列表 list1 拼接到 list 之后,得到 list = "); | ||
print_util::print_array(&list); | ||
|
||
// 排序列表 | ||
list.sort(); | ||
print!("\n排序列表后 list = "); | ||
print_util::print_array(&list); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,40 +4,40 @@ | |
* Author: xBLACICEx ([email protected]), sjinzh ([email protected]) | ||
*/ | ||
|
||
include!("../include/include.rs"); | ||
include!("../include/include.rs"); | ||
|
||
use rand::seq::SliceRandom; | ||
use rand::thread_rng; | ||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ | ||
fn random_numbers(n: i32) -> Vec<i32> { | ||
// 生成数组 nums = { 1, 2, 3, ..., n } | ||
let mut nums = (1..=n).collect::<Vec<i32>>(); | ||
// 随机打乱数组元素 | ||
nums.shuffle(&mut thread_rng()); | ||
nums | ||
} | ||
/* 查找数组 nums 中数字 1 所在索引 */ | ||
fn find_one(nums: &[i32]) -> Option<usize> { | ||
for i in 0..nums.len() { | ||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) | ||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) | ||
if nums[i] == 1 { | ||
return Some(i); | ||
} | ||
} | ||
None | ||
} | ||
/* Driver Code */ | ||
fn main() { | ||
for _ in 0..10 { | ||
let n = 100; | ||
let nums = random_numbers(n); | ||
let index = find_one(&nums).unwrap(); | ||
print!("\n数组 [ 1, 2, ..., n ] 被打乱后 = "); | ||
print_util::print_array(&nums); | ||
println!("\n数字 1 的索引为 {}", index); | ||
} | ||
} | ||
use rand::seq::SliceRandom; | ||
use rand::thread_rng; | ||
|
||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ | ||
fn random_numbers(n: i32) -> Vec<i32> { | ||
// 生成数组 nums = { 1, 2, 3, ..., n } | ||
let mut nums = (1..=n).collect::<Vec<i32>>(); | ||
// 随机打乱数组元素 | ||
nums.shuffle(&mut thread_rng()); | ||
nums | ||
} | ||
|
||
/* 查找数组 nums 中数字 1 所在索引 */ | ||
fn find_one(nums: &[i32]) -> Option<usize> { | ||
for i in 0..nums.len() { | ||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) | ||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) | ||
if nums[i] == 1 { | ||
return Some(i); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/* Driver Code */ | ||
fn main() { | ||
for _ in 0..10 { | ||
let n = 100; | ||
let nums = random_numbers(n); | ||
let index = find_one(&nums).unwrap(); | ||
print!("\n数组 [ 1, 2, ..., n ] 被打乱后 = "); | ||
print_util::print_array(&nums); | ||
println!("\n数字 1 的索引为 {}", index); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.