feat: add rust docs (#815)

* feat: add rust docs

* Import std types for built_in_hash doc
This commit is contained in:
易春风 2023-10-01 19:25:03 +08:00 committed by GitHub
parent e8bf5879b0
commit cbe76b58a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 162 additions and 12 deletions

View file

@ -532,7 +532,27 @@
=== "Rust"
```rust title=""
/* 回溯算法框架 */
fn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {
// 判断是否为解
if is_solution(state) {
// 记录解
record_solution(state, res);
// 停止继续搜索
return;
}
// 遍历所有选择
for choice in choices {
// 剪枝:判断选择是否合法
if is_valid(state, choice) {
// 尝试:做出选择,更新状态
make_choice(state, choice);
backtrack(state, choices, res);
// 回退:撤销选择,恢复到之前的状态
undo_choice(state, choice);
}
}
}
```
=== "C"

View file

@ -135,7 +135,11 @@
=== "Rust"
```rust title=""
// 使用多种基本数据类型来初始化数组
let numbers: Vec<i32> = vec![0; 5];
let decimals: Vec<float> = vec![0.0, 5];
let characters: Vec<char> = vec!['0'; 5];
let bools: Vec<bool> = vec![false; 5];
```
=== "C"

View file

@ -436,6 +436,45 @@ $$
=== "Rust"
```rust title="built_in_hash.rs"
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let num = 3;
let mut num_hasher = DefaultHasher::new();
num.hash(&mut num_hasher);
let hash_num = num_hasher.finish();
// 整数 3 的哈希值为 568126464209439262
let bol = true;
let mut bol_hasher = DefaultHasher::new();
bol.hash(&mut bol_hasher);
let hash_bol = bol_hasher.finish();
// 布尔量 true 的哈希值为 4952851536318644461
let dec: f32 = 3.14159;
let mut dec_hasher = DefaultHasher::new();
dec.to_bits().hash(&mut dec_hasher);
let hash_dec = dec_hasher.finish();
println!("小数 {} 的哈希值为 {}", dec, hash_dec);
// 小数 3.14159 的哈希值为 2566941990314602357
let str = "Hello 算法";
let mut str_hasher = DefaultHasher::new();
str.hash(&mut str_hasher);
let hash_str = str_hasher.finish();
// 字符串 Hello 算法 的哈希值为 16092673739211250988
let arr = (&12836, &"小哈");
let mut tup_hasher = DefaultHasher::new();
arr.hash(&mut tup_hasher);
let hash_tup = tup_hasher.finish();
// 元组 (12836, "小哈") 的哈希值为 1885128010422702749
let node = ListNode::new(42);
let mut hasher = DefaultHasher::new();
node.borrow().val.hash(&mut hasher);
let hash = hasher.finish();
// 节点对象 RefCell { value: ListNode { val: 42, next: None } } 的哈希值为15387811073369036852
```

View file

@ -296,7 +296,41 @@
=== "Rust"
```rust title="heap.rs"
use std::collections::BinaryHeap;
use std::cmp::Reverse;
/* 初始化堆 */
// 初始化小顶堆
let mut min_heap = BinaryHeap::<Reverse<i32>>::new();
// 初始化大顶堆
let mut max_heap = BinaryHeap::new();
/* 元素入堆 */
max_heap.push(1);
max_heap.push(3);
max_heap.push(2);
max_heap.push(5);
max_heap.push(4);
/* 获取堆顶元素 */
let peek = max_heap.peek().unwrap(); // 5
/* 堆顶元素出堆 */
// 出堆元素会形成一个从大到小的序列
let peek = max_heap.pop().unwrap(); // 5
let peek = max_heap.pop().unwrap(); // 4
let peek = max_heap.pop().unwrap(); // 3
let peek = max_heap.pop().unwrap(); // 2
let peek = max_heap.pop().unwrap(); // 1
/* 获取堆大小 */
let size = max_heap.len();
/* 判断堆是否为空 */
let is_empty = max_heap.is_empty();
/* 输入列表并建堆 */
let min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);
```
=== "C"

View file

@ -278,20 +278,16 @@
stack.push(4);
/* 访问栈顶元素 */
if let Some(top) = stack.get(stack.len() - 1) {
}
if let Some(top) = stack.last() {
}
let top = stack[stack.len() - 1];
/* 元素出栈 */
if let Some(pop) = stack.pop() {
}
let pop = stack.pop().unwrap();
/* 获取栈的长度 */
let size = stack.len();
/* 判断是否为空 */
let isEmpty = stack.is_empty();
let is_empty = stack.is_empty();
```
=== "C"

View file

@ -99,7 +99,9 @@
=== "Rust"
```rust title=""
/* 二叉树的数组表示 */
// 使用 None 来标记空位
let tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];
```
=== "C"

View file

@ -153,7 +153,28 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
=== "Rust"
```rust title=""
use std::rc::Rc;
use std::cell::RefCell;
/* AVL 树节点类型 */
struct TreeNode {
val: i32, // 节点值
height: i32, // 节点高度
left: Option<Rc<RefCell<TreeNode>>>, // 左子节点
right: Option<Rc<RefCell<TreeNode>>>, // 右子节点
}
impl TreeNode {
/* AVL 树节点构造方法 */
fn new(val: i32) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
val,
height: 0,
left: None,
right: None
}))
}
}
```
=== "C"

View file

@ -126,7 +126,26 @@
=== "Rust"
```rust title=""
use std::rc::Rc;
use std::cell::RefCell;
/* 二叉树节点类型 */
struct TreeNode {
val: i32, // 节点值
left: Option<Rc<RefCell<TreeNode>>>, // 左子节点引用
right: Option<Rc<RefCell<TreeNode>>>, // 右子节点引用
}
impl TreeNode {
/* 二叉树节点构造方法 */
fn new(val: i32) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self {
val,
left: None,
right: None
}))
}
}
```
=== "C"
@ -346,7 +365,17 @@
=== "Rust"
```rust title="binary_tree.rs"
// 初始化节点
let n1 = TreeNode::new(1);
let n2 = TreeNode::new(2);
let n3 = TreeNode::new(3);
let n4 = TreeNode::new(4);
let n5 = TreeNode::new(5);
// 构建引用指向(即指针)
n1.borrow_mut().left = Some(n2.clone());
n1.borrow_mut().right = Some(n3);
n2.borrow_mut().left = Some(n4);
n2.borrow_mut().right = Some(n5);
```
=== "C"
@ -487,7 +516,12 @@
=== "Rust"
```rust title="binary_tree.rs"
let p = TreeNode::new(0);
// 在 n1 -> n2 中间插入节点 P
n1.borrow_mut().left = Some(p.clone());
p.borrow_mut().left = Some(n2.clone());
// 删除节点 p
n1.borrow_mut().left = Some(n2);
```
=== "C"