diff --git a/codes/rust/chapter_array_and_linkedlist/array.rs b/codes/rust/chapter_array_and_linkedlist/array.rs index 2d6870b77..29241d2ec 100644 --- a/codes/rust/chapter_array_and_linkedlist/array.rs +++ b/codes/rust/chapter_array_and_linkedlist/array.rs @@ -21,9 +21,8 @@ fn extend(nums: &[i32], enlarge: usize) -> Vec { // 初始化一个扩展长度后的数组 let mut res: Vec = vec![0; nums.len() + enlarge]; // 将原数组中的所有元素复制到新 - for i in 0..nums.len() { - res[i] = nums[i]; - } + res[0..nums.len()].copy_from_slice(nums); + // 返回扩展后的新数组 res } @@ -54,7 +53,8 @@ fn traverse(nums: &[i32]) { _count += nums[i]; } // 直接遍历数组元素 - for num in nums { + _count = 0; + for &num in nums { _count += num; } } diff --git a/codes/rust/chapter_array_and_linkedlist/linked_list.rs b/codes/rust/chapter_array_and_linkedlist/linked_list.rs index 601a99714..99ac5c530 100644 --- a/codes/rust/chapter_array_and_linkedlist/linked_list.rs +++ b/codes/rust/chapter_array_and_linkedlist/linked_list.rs @@ -19,9 +19,6 @@ pub fn insert(n0: &Rc>>, P: Rc>>) { /* 删除链表的节点 n0 之后的首个节点 */ #[allow(non_snake_case)] pub fn remove(n0: &Rc>>) { - if n0.borrow().next.is_none() { - return; - }; // n0 -> P -> n1 let P = n0.borrow_mut().next.take(); if let Some(node) = P { @@ -31,26 +28,39 @@ pub fn remove(n0: &Rc>>) { } /* 访问链表中索引为 index 的节点 */ -pub fn access(head: Rc>>, index: i32) -> Rc>> { - if index <= 0 { - return head; - }; - if let Some(node) = &head.borrow().next { - return access(node.clone(), index - 1); +pub fn access(head: Rc>>, index: i32) -> Option>>> { + fn dfs( + head: Option<&Rc>>>, + index: i32, + ) -> Option>>> { + if index <= 0 { + return head.cloned(); + } + + if let Some(node) = head { + dfs(node.borrow().next.as_ref(), index - 1) + } else { + None + } } - return head; + dfs(Some(head).as_ref(), index) } /* 在链表中查找值为 target 的首个节点 */ -pub fn find(head: Rc>>, target: T, index: i32) -> i32 { - if head.borrow().val == target { - return index; - }; - if let Some(node) = &head.borrow_mut().next { - return find(node.clone(), target, index + 1); +pub fn find(head: Rc>>, target: T) -> i32 { + fn find(head: Option<&Rc>>>, target: T, idx: i32) -> i32 { + if let Some(node) = head { + if node.borrow().val == target { + return idx; + } + return find(node.borrow().next.as_ref(), target, idx + 1); + } else { + -1 + } } - return -1; + + find(Some(head).as_ref(), target, 0) } /* Driver Code */ @@ -82,9 +92,9 @@ fn main() { /* 访问节点 */ let node = access(n0.clone(), 3); - println!("链表中索引 3 处的节点的值 = {}", node.borrow().val); + println!("链表中索引 3 处的节点的值 = {}", node.unwrap().borrow().val); /* 查找节点 */ - let index = find(n0.clone(), 2, 0); + let index = find(n0.clone(), 2); println!("链表中值为 2 的节点的索引 = {}", index); } diff --git a/codes/rust/src/include/vertex.rs b/codes/rust/src/include/vertex.rs index c4e941e6c..ca1a5a821 100644 --- a/codes/rust/src/include/vertex.rs +++ b/codes/rust/src/include/vertex.rs @@ -10,9 +10,15 @@ pub struct Vertex { pub val: i32, } +impl From for Vertex { + fn from(value: i32) -> Self { + Self { val: value } + } +} + /* 输入值列表 vals ,返回顶点列表 vets */ pub fn vals_to_vets(vals: Vec) -> Vec { - vals.into_iter().map(|val| Vertex { val }).collect() + vals.into_iter().map(|val| val.into()).collect() } /* 输入顶点列表 vets ,返回值列表 vals */