cargo fmt rust code (#1131)

* cargo fmt code

* Add empty line to seperate unrelated comments

* Fix review

* Update bubble_sort.rs

* Update merge_sort.rs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
rongyi
2024-03-16 02:13:41 +08:00
committed by GitHub
parent 54ceef3443
commit 7b1094318b
70 changed files with 1021 additions and 836 deletions

View File

@@ -79,7 +79,7 @@ fn main() {
// 在 Rust 中,指定长度时([i32; 5])为数组
// 由于 Rust 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
// 为了方便实现扩容 extend() 方法,以下将(Vec) 看作数组Array也是rust一般情况下使用动态数组的类型
let nums = vec![ 1, 3, 2, 5, 4 ];
let nums = vec![1, 3, 2, 5, 4];
print!("\n数组 nums = ");
print_util::print_array(&nums);

View File

@@ -6,14 +6,14 @@
include!("../include/include.rs");
use std::rc::Rc;
use std::cell::RefCell;
use list_node::ListNode;
use std::cell::RefCell;
use std::rc::Rc;
/* 在链表的节点 n0 之后插入节点 P */
#[allow(non_snake_case)]
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
let n1 = n0.borrow_mut().next.take();
let n1 = n0.borrow_mut().next.take();
P.borrow_mut().next = n1;
n0.borrow_mut().next = Some(P);
}
@@ -21,7 +21,9 @@ pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
/* 删除链表的节点 n0 之后的首个节点 */
#[allow(non_snake_case)]
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
if n0.borrow().next.is_none() {return};
if n0.borrow().next.is_none() {
return;
};
// n0 -> P -> n1
let P = n0.borrow_mut().next.take();
if let Some(node) = P {
@@ -32,7 +34,9 @@ pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
/* 访问链表中索引为 index 的节点 */
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
if index <= 0 {return head};
if index <= 0 {
return head;
};
if let Some(node) = &head.borrow_mut().next {
return access(node.clone(), index - 1);
}
@@ -41,7 +45,9 @@ pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListN
/* 在链表中查找值为 target 的首个节点 */
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
if head.borrow().val == target {return index};
if head.borrow().val == target {
return index;
};
if let Some(node) = &head.borrow_mut().next {
return find(node.clone(), target, index + 1);
}
@@ -51,7 +57,7 @@ pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32)
/* Driver Code */
fn main() {
/* 初始化链表 */
// 初始化各个节点
// 初始化各个节点
let n0 = ListNode::new(1);
let n1 = ListNode::new(3);
let n2 = ListNode::new(2);

View File

@@ -9,7 +9,7 @@ include!("../include/include.rs");
/* Driver Code */
fn main() {
// 初始化列表
let mut nums: Vec<i32> = vec![ 1, 3, 2, 5, 4 ];
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
print!("列表 nums = ");
print_util::print_array(&nums);
@@ -58,9 +58,10 @@ fn main() {
}
// 拼接两个列表
let mut nums1 = vec![ 6, 8, 7, 10, 9 ];
nums.append(&mut nums1); // append移动 之后 nums1 为空!
// nums.extend(&nums1); // extend借用 nums1 能继续使用
let mut nums1 = vec![6, 8, 7, 10, 9];
nums.append(&mut nums1); // append移动 之后 nums1 为空!
// nums.extend(&nums1); // extend借用 nums1 能继续使用
print!("\n将列表 nums1 拼接到 nums 之后,得到 nums = ");
print_util::print_array(&nums);

View File

@@ -10,16 +10,16 @@ include!("../include/include.rs");
#[allow(dead_code)]
struct MyList {
arr: Vec<i32>, // 数组(存储列表元素)
capacity: usize, // 列表容量
size: usize, // 列表长度(当前元素数量)
extend_ratio: usize, // 每次列表扩容的倍数
capacity: usize, // 列表容量
size: usize, // 列表长度(当前元素数量)
extend_ratio: usize, // 每次列表扩容的倍数
}
#[allow(unused,unused_comparisons)]
#[allow(unused, unused_comparisons)]
impl MyList {
/* 构造方法 */
pub fn new(capacity: usize) -> Self {
let mut vec = Vec::new();
let mut vec = Vec::new();
vec.resize(capacity, 0);
Self {
arr: vec,
@@ -42,13 +42,17 @@ impl MyList {
/* 访问元素 */
pub fn get(&self, index: usize) -> i32 {
// 索引如果越界,则抛出异常,下同
if index >= self.size {panic!("索引越界")};
if index >= self.size {
panic!("索引越界")
};
return self.arr[index];
}
/* 更新元素 */
pub fn set(&mut self, index: usize, num: i32) {
if index >= self.size {panic!("索引越界")};
if index >= self.size {
panic!("索引越界")
};
self.arr[index] = num;
}
@@ -65,7 +69,9 @@ impl MyList {
/* 在中间插入元素 */
pub fn insert(&mut self, index: usize, num: i32) {
if index >= self.size() {panic!("索引越界")};
if index >= self.size() {
panic!("索引越界")
};
// 元素数量超出容量时,触发扩容机制
if self.size == self.capacity() {
self.extend_capacity();
@@ -81,7 +87,9 @@ impl MyList {
/* 删除元素 */
pub fn remove(&mut self, index: usize) -> i32 {
if index >= self.size() {panic!("索引越界")};
if index >= self.size() {
panic!("索引越界")
};
let num = self.arr[index];
// 将将索引 index 之后的元素都向前移动一位
for j in (index..self.size - 1) {