Rust Code Guidelines
Under Construction
This section of the documentation is not formalized and may change.
Help us by contributing to it on our GitHub.
Help us by contributing to it on our GitHub.
Names
Use pascal case for structs and snake case for variables and functions.
tip
When defining a const variable always write it in UPPERCASE
Do ✅:
struct Example;
fn example_func() -> &str;
let example_var: i32;
const EXAMPLE_VAR;
Don't ❌:
struct example;
fn ExampleFunc() -> &str; // fn exampleFUNC() -> &str;
let exampleVar: i32;
const example_var;
Use full words, except when the variable name is too long.
Do ✅:
let my_var: i64;
let length: u8;
let character: char;
Don't ❌:
let my_variable: i64;
let len: u8;
let character: char;
Use descriptive names in functions.
Do ✅:
fn convert_to_ascii(let my_var: i32) -> char;
Don't ❌:
fn to_ascii(let my_var: i32) -> i32;
Variables
Declare EXPLICITLY the type of a variable.
caution
Explicit types are not obligatory, but are recommended
Do ✅:
let var: f64 = 7.42;
Don't ❌:
let var = 7.42
When printing a simple variable value don't do the traditional way. Why? Because it's less write time
Do ✅:
let x = 4;
println!("{x}");
Don't ❌:
let x = 4;
println!("{}", x);
Statements
If the code is not too long, write the statement in a single line
Do ✅:
loop { println!("This phrase never stops!") }
let x = 5;
let y = if x == 5 { 10 } else { 20 };
Don't ❌:
loop {
println!("This phrase never stops!")
}
let x = 5;
let y = if x == 5 {
10
} else {
20
};
Functions
When returning a value just type the name of delete the return keyword
Do ✅:
fn function(x: i32) -> i32 {
x
}
Don't ❌:
fn function(x: i32) -> i32 {
return x;
}
Imports
When importing multiple variables separate any library in multiple lines
tip
Sort the imports by name and always add a ,
at the end of a library
Do ✅:
use clap::{
Arg,
ArgAction,
Command,
};
Don't ❌:
use x::{Command, Arg, ArgAction};