Skip to content

chore: add rust formatter #1946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .github/workflows/pr-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

我们项目中各种编程语言代码(包括文档)所采用的格式化工具不同,提交 pr 之前必须确保代码、文档正确格式化。

- .{md,js,ts,php,sql} 采用 prettier
- .{md,js,ts,php,sql,rs} 采用 prettier
- .{c,cpp,java} 采用 clang-format
- .{py} 采用 black
- .{go} 采用 gofmt
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:

We use different formatting tools for various programming languages (including documentation) in our project. You must ensure that the code and documentation are correctly formatted before submitting a pr.

- .{md,js,ts,php,sql} use prettier
- .{md,js,ts,php,sql,rs} use prettier
- .{c,cpp,java} use clang-format
- .{py} use black
- .{go} use gofmt
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Run prettier
run: |
git config --global core.quotepath off
changed_files=$(git diff --name-only "${{ github.event.pull_request.base.sha }}" | grep -E '\.md$|\.js$|\.ts$|\.php|\.sql$' || true)
changed_files=$(git diff --name-only "${{ github.event.pull_request.base.sha }}" | grep -E '\.js$|\.ts$|\.php$|\.sql$|\.rs$|\.md$' || true)
if [ -n "$changed_files" ]; then
echo "Running prettier on the changed files"
echo "$changed_files" | xargs -d '\n' npx prettier --write
Expand Down
10 changes: 8 additions & 2 deletions basic/sorting/HeapSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,19 @@ fn sink(nums: &mut Vec<i32>, mut i: usize, n: usize) {
fn main() -> io::Result<()> {
let mut s = String::new();
io::stdin().read_line(&mut s)?;
let s: Vec<usize> = s.split(' ').map(|s| s.trim().parse().unwrap()).collect();
let s: Vec<usize> = s
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();
// let n = s[0];
let m = s[1];

let mut nums = String::new();
io::stdin().read_line(&mut nums)?;
let mut nums: Vec<i32> = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect();
let mut nums: Vec<i32> = nums
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();

heap_sort(&mut nums);
for num in nums.iter().take(m) {
Expand Down
2 changes: 1 addition & 1 deletion basic/sorting/InsertionSort/InsertionSort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fn insertion_sort(nums: &mut Vec<i32>) {
for i in 1..n {
let mut j = i - 1;
let temp = nums[i];
while j >= 0 as usize && nums[j] > temp {
while j >= (0 as usize) && nums[j] > temp {
nums[j + 1] = nums[j];
j -= 1;
}
Expand Down
2 changes: 1 addition & 1 deletion basic/sorting/InsertionSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn insertion_sort(nums: &mut Vec<i32>) {
for i in 1..n {
let mut j = i - 1;
let temp = nums[i];
while j >= 0 as usize && nums[j] > temp {
while j >= (0 as usize) && nums[j] > temp {
nums[j + 1] = nums[j];
j -= 1;
}
Expand Down
5 changes: 4 additions & 1 deletion basic/sorting/MergeSort/Main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ fn main() -> io::Result<()> {

let mut nums = String::new();
io::stdin().read_line(&mut nums)?;
let mut nums: Vec<i32> = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect();
let mut nums: Vec<i32> = nums
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();

merge_sort(&mut nums, 0, n - 1);
for num in nums.iter() {
Expand Down
5 changes: 4 additions & 1 deletion basic/sorting/MergeSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ fn main() -> io::Result<()> {

let mut nums = String::new();
io::stdin().read_line(&mut nums)?;
let mut nums: Vec<i32> = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect();
let mut nums: Vec<i32> = nums
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();

merge_sort(&mut nums, 0, n - 1);
for num in nums.iter() {
Expand Down
5 changes: 4 additions & 1 deletion basic/sorting/QuickSort/Main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ fn main() -> io::Result<()> {

let mut nums = String::new();
io::stdin().read_line(&mut nums)?;
let mut nums: Vec<i32> = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect();
let mut nums: Vec<i32> = nums
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();

quick_sort(&mut nums, 0, n - 1);
for num in nums.iter() {
Expand Down
5 changes: 4 additions & 1 deletion basic/sorting/QuickSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ fn main() -> io::Result<()> {

let mut nums = String::new();
io::stdin().read_line(&mut nums)?;
let mut nums: Vec<i32> = nums.split(' ').map(|s| s.trim().parse().unwrap()).collect();
let mut nums: Vec<i32> = nums
.split(' ')
.map(|s| s.trim().parse().unwrap())
.collect();

quick_sort(&mut nums, 0, n - 1);
for num in nums.iter() {
Expand Down
2 changes: 1 addition & 1 deletion basic/sorting/ShellSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn shell_sort(nums: &mut Vec<i32>) {
for i in gap..n {
let mut j = i - gap;
let temp = nums[i];
while j >= 0 as usize && nums[j] > temp {
while j >= (0 as usize) && nums[j] > temp {
nums[j + gap] = nums[j];
j -= gap;
}
Expand Down
2 changes: 1 addition & 1 deletion basic/sorting/ShellSort/ShellSort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn shell_sort(nums: &mut Vec<i32>) {
for i in gap..n {
let mut j = i - gap;
let temp = nums[i];
while j >= 0 as usize && nums[j] > temp {
while j >= (0 as usize) && nums[j] > temp {
nums[j + gap] = nums[j];
j -= gap;
}
Expand Down
6 changes: 1 addition & 5 deletions lcci/01.03.String to URL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ impl Solution {
s.chars()
.take(length as usize)
.map(|c| {
if c == ' ' {
"%20".to_string()
} else {
c.to_string()
}
if c == ' ' { "%20".to_string() } else { c.to_string() }
})
.collect()
}
Expand Down
6 changes: 1 addition & 5 deletions lcci/01.03.String to URL/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ impl Solution {
s.chars()
.take(length as usize)
.map(|c| {
if c == ' ' {
"%20".to_string()
} else {
c.to_string()
}
if c == ' ' { "%20".to_string() } else { c.to_string() }
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion lcci/01.09.String Rotation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Solution {
}
if is_pass {
return true;
};
}
}
false
}
Expand Down
2 changes: 1 addition & 1 deletion lcci/01.09.String Rotation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Solution {
}
if is_pass {
return true;
};
}
}
false
}
Expand Down
2 changes: 1 addition & 1 deletion lcci/02.05.Sum Lists/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul
impl Solution {
pub fn add_two_numbers(
mut l1: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>
) -> Option<Box<ListNode>> {
let mut dummy = Some(Box::new(ListNode::new(0)));
let mut cur = dummy.as_mut();
Expand Down
2 changes: 1 addition & 1 deletion lcci/02.05.Sum Lists/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul
impl Solution {
pub fn add_two_numbers(
mut l1: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>
) -> Option<Box<ListNode>> {
let mut dummy = Some(Box::new(ListNode::new(0)));
let mut cur = dummy.as_mut();
Expand Down
2 changes: 1 addition & 1 deletion lcci/02.05.Sum Lists/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
impl Solution {
pub fn add_two_numbers(
mut l1: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>
) -> Option<Box<ListNode>> {
let mut dummy = Some(Box::new(ListNode::new(0)));
let mut cur = dummy.as_mut();
Expand Down
6 changes: 1 addition & 5 deletions lcci/03.02.Min Stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,11 @@ struct MinStack {
min_stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {

/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
Expand All @@ -269,9 +267,7 @@ impl MinStack {
fn get_min(&self) -> i32 {
*self.min_stack.back().unwrap()
}
}

/**
}/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(x);
Expand Down
6 changes: 1 addition & 5 deletions lcci/03.02.Min Stack/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,11 @@ struct MinStack {
min_stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {

/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
Expand All @@ -269,9 +267,7 @@ impl MinStack {
fn get_min(&self) -> i32 {
*self.min_stack.back().unwrap()
}
}

/**
}/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(x);
Expand Down
8 changes: 2 additions & 6 deletions lcci/03.02.Min Stack/Solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ struct MinStack {
min_stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {

/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
Expand All @@ -37,13 +35,11 @@ impl MinStack {
fn get_min(&self) -> i32 {
*self.min_stack.back().unwrap()
}
}

/**
}/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(x);
* obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: i32 = obj.get_min();
*/
*/
8 changes: 2 additions & 6 deletions lcci/03.05.Sort of Stacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,14 @@ func (s *SortedStack) IsEmpty() bool {
```rust
use std::collections::VecDeque;
struct SortedStack {
stack: VecDeque<i32>
stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl SortedStack {

fn new() -> Self {
Self { stack: VecDeque::new() }
}
Expand All @@ -281,9 +279,7 @@ impl SortedStack {
fn is_empty(&self) -> bool {
self.stack.is_empty()
}
}

/**
}/**
* Your SortedStack object will be instantiated and called as such:
* let obj = SortedStack::new();
* obj.push(val);
Expand Down
8 changes: 2 additions & 6 deletions lcci/03.05.Sort of Stacks/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,14 @@ func (s *SortedStack) IsEmpty() bool {
```rust
use std::collections::VecDeque;
struct SortedStack {
stack: VecDeque<i32>
stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl SortedStack {

fn new() -> Self {
Self { stack: VecDeque::new() }
}
Expand All @@ -286,9 +284,7 @@ impl SortedStack {
fn is_empty(&self) -> bool {
self.stack.is_empty()
}
}

/**
}/**
* Your SortedStack object will be instantiated and called as such:
* let obj = SortedStack::new();
* obj.push(val);
Expand Down
10 changes: 3 additions & 7 deletions lcci/03.05.Sort of Stacks/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::collections::VecDeque;
struct SortedStack {
stack: VecDeque<i32>
stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl SortedStack {

fn new() -> Self {
Self { stack: VecDeque::new() }
}
Expand All @@ -35,13 +33,11 @@ impl SortedStack {
fn is_empty(&self) -> bool {
self.stack.is_empty()
}
}

/**
}/**
* Your SortedStack object will be instantiated and called as such:
* let obj = SortedStack::new();
* obj.push(val);
* obj.pop();
* let ret_3: i32 = obj.peek();
* let ret_4: bool = obj.is_empty();
*/
*/
Loading