fix: check the rust codes and fix them (#653)

* fix: check the rust codes and fix it

* Update binary_tree_bfs.rs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
Night Cruising
2023-07-24 22:27:26 +08:00
committed by GitHub
parent 978d3c2ed7
commit fdbe275fc9
18 changed files with 167 additions and 102 deletions

View File

@@ -72,6 +72,7 @@ fn find(nums: &[i32], target: i32) -> Option<usize> {
/* Driver Code */
fn main() {
/* 初始化数组 */
let arr = [0; 5];
print!("数组 arr = ");
print_util::print_array(&arr);

View File

@@ -52,11 +52,12 @@
_count += 1;
}
// 直接遍历列表元素
// 直接遍历列表元素
_count = 0;
for _ in &list {
for _n in &list {
_count += 1;
} // 或者
}
// 或者
// list.iter().for_each(|_| _count += 1);
// let _count = list.iter().fold(0, |_count, _| _count + 1);

View File

@@ -8,10 +8,10 @@ include!("../include/include.rs");
#[allow(dead_code)]
struct MyList {
nums: Vec<i32>,
capacity: usize,
size: usize,
extend_ratio: usize,
nums: Vec<i32>, // 数组(存储列表元素)
capacity: usize, // 列表容量
size: usize, // 列表长度(即当前元素数量)
extend_ratio: usize, // 每次列表扩容的倍数
}
#[allow(unused,unused_comparisons)]
@@ -94,6 +94,7 @@ impl MyList {
/* 列表扩容 */
pub fn extend_capacity(&mut self) {
// 新建一个长度为原数组 extend_ratio 倍的新数组,并将原数组拷贝到新数组
let new_capacity = self.capacity * self.extend_ratio;
self.nums.resize(new_capacity, 0);
// 更新列表容量
@@ -102,6 +103,7 @@ impl MyList {
/* 将列表转换为数组 */
pub fn to_array(&mut self) -> Vec<i32> {
// 仅转换有效长度范围内的列表元素
let mut nums = Vec::new();
for i in 0..self.size {
nums.push(self.get(i));