Clean code in Rust
The Rust Style Guide: types in UpperCamelCase, functions and variables in snake_case, constants in SCREAMING_SNAKE_CASE, 4 spaces a level and lines of 100 characters at most; rustfmt takes the guide as its default.
What does the Rust Style Guide ask for?
Prefer to use Rust’s expression oriented nature where possible;
source
What else does it ask?
- Spaces, not tabs. source
What does cleaner Rust look like?
const maxRetries: u32 = 3;
struct http_client { retryCount: u32 }
fn shouldRetry(c: &http_client) -> bool {
let r;
if c.retryCount < maxRetries { r = true; } else { r = false; }
return r;
}const MAX_RETRIES: u32 = 3;
struct HttpClient {
retry_count: u32,
}
fn should_retry(client: &HttpClient) -> bool {
client.retry_count < MAX_RETRIES
}Other languages
More on clean code: comments, functions, naming and the checklist.