Skip to content

Commit 9e02aff

Browse files
authored
Merge pull request Rust-Coding-Guidelines#10 from PeterWrighten/main
Update: 3.9 FUN-D && Contents
2 parents 858885d + cd908c3 commit 9e02aff

File tree

7 files changed

+103
-3
lines changed

7 files changed

+103
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# 异步编程
1+
# Async
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# 包管理
1+
# Cargo package management
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# 函数设计
1+
# Function Design
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
11
# G.FUD.01 Less than 5 function parameters
2+
3+
**[Level] Advice**
4+
5+
**[Description]**
6+
7+
In order to improve readability of code, it's better to set less than 5 function's parameters.
8+
9+
10+
**[Bad Case]**
11+
12+
```rust
13+
struct Color;
14+
// Not Good
15+
fn foo(x: f32, y: f32, name: &str, c: Color, w: u32, h: u32, a: u32, b: u32) {
16+
// ..
17+
}
18+
```
19+
20+
**[Good Case]**
21+
22+
Refine function's parameters as possible.
23+
24+
```rust
25+
struct Color;
26+
// Good: use const generic here to receive multiple u32 typed parameters afterward.
27+
// use tuple to refine 2~3 parameters as one.
28+
fn foo<T, const N: usize>(x: (f32, f32), name: &str, c: Color, last: [T;N]) {
29+
;
30+
}
31+
32+
fn main() {
33+
let arr = [1u32, 2u32];
34+
foo((1.0f32, 2.0f32), "hello", Color, arr);
35+
let arr = [1.0f32, 2.0f32, 3.0f32];
36+
foo((1.0f32, 2.0f32), "hello", Color, arr);
37+
}
38+
```
39+
40+
**[Lint Check]**
41+
42+
| lint name | Clippy check | Rustc Check | Lint Group | level |
43+
| --------- | ------------ | ----------- | ---------- | ----- |
44+
| [too_many_arguments](https://rust-lang.github.io/rust-clippy/master/#too_many_arguments) | yes | no | complexity | warn |
45+
46+
This lint corresponds to the following configuration of `clippy.toml`:
47+
48+
```toml
49+
# less than 5 parameters
50+
too-many-arguments-threshold=5
51+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
11
# G.FUD.02 Should input as reference if parameter derives `Copy` and its value inputed by-value is a big number
2+
3+
**[Level] Advice**
4+
5+
**[Description]**
6+
7+
By-value inputed parameters may emerge unnecessary `memcpy`, which could kill performance.
8+
9+
**[Bad Case]**
10+
11+
```rust
12+
#![warn(clippy::large_types_passed_by_value)]
13+
14+
#[derive(Clone, Copy)]
15+
struct TooLarge([u8; 2048]);
16+
17+
// Not Good
18+
fn foo(v: TooLarge) {}
19+
```
20+
21+
**[Good Case]**
22+
23+
```rust
24+
#![warn(clippy::large_types_passed_by_value)]
25+
26+
#[derive(Clone, Copy)]
27+
struct TooLarge([u8; 2048]);
28+
29+
// Good
30+
fn foo(v: &TooLarge) {}
31+
```
32+
33+
**[Lint Check]**
34+
35+
| lint name | Clippy check | Rustc check | Lint Group | Level |
36+
| --------- | ------------ | ----------- | ---------- | ----- |
37+
| [large_types_passed_by_value](https://rust-lang.github.io/rust-clippy/master/#large_types_passed_by_value) | yes | no | pedanic | allow |
38+
39+
This lint corresponds to the following configuration of `clippy.toml`
40+
41+
```toml
42+
# if function is an exported api, then the lint would not be triggered, which aims to prevent devastated change for api. the default is true.
43+
avoid-breaking-exported-api=true
44+
```

src/safe-guides/coding_practice/fn-design/P.FUD.01.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# P.FUD.01 Should rebind variables which pass into closure
22

33
**[Description]**
4+
45
By default, closure captures environment variables with `borrow`, or it is also allowed to pass environment variables into closure with `move` keyword.
56

67
It is more readable to rebind and regroup these variables which pass into closure.
78

89
**[Bad Case]**
10+
911
```rust
1012
use std::rc::Rc;
1113
let num1 = Rc::new(1);
@@ -23,6 +25,7 @@ let closure = {
2325

2426

2527
**[Good Case]**
28+
2629
```rust
2730
use std::rc::Rc;
2831

src/safe-guides/coding_practice/fn-design/P.FUD.02.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# P.FUD.02 Don't Use `return` for return value
22

33
**[Description]**
4+
45
In such that function would automatically return the value of last expression in Rust, it is not required to use `return` explicitly.
56

67
Only when it is necessary to return a value in procedure of function, should use `return` explicitly.
78

89
**[Bad Case]**
10+
911
```rust
1012
fn foo(x: usize) -> usize {
1113
if x < 42 {
@@ -14,7 +16,9 @@ fn foo(x: usize) -> usize {
1416
return x + 1; // Not Good
1517
}
1618
```
19+
1720
**[Good Case]**
21+
1822
```rust
1923
fn foo(x: usize) -> usize {
2024
if x < 42 {

0 commit comments

Comments
 (0)