I am new to Rust and wanted to know if there is a recommended compiling setting (like gcc -Wall) to capture warnings. The --help shows -W, how is used. Is there a better approach?
D:\rust\bin>rustc -W block.rs
error: no input filename given
I did find the warnings quite clear when there is a potential issue. In this case the last 'x' variable is bound, I think Ruby is the same. A Rubyist can confirm.
D:\rust\bin>rustc block.rs
block.rs:2:9: 2:10 warning: unused variable: x, #[warn(unused_variables)] on b
y default
block.rs:2 let x: u32 = 17;
^
D:\rust\bin>block
The value of x is 12 and value of y is 3
fn main() {
let x: u32 = 17;
let x = 5;
{
let y: i32 = 3;
println!("The value of x is {} and value of y is {}", x, y);
}
}
D:\rust\bin>block
The value of x is 5 and value of y is 3
First of all, you could surround your code in tripple backquotes ```code``` to make it properly highlighted:
fn main() {
println!("Hello, World");
}
And you can edit your existing post to fix this
Regarding warnings, Rust has a good set of warnings enabled by default, and I think that the recommended setting is just to rely on those defaults.
If you want even more scrutiny from the compiler, you can add #![deny(warnings)] to the top of your file. This will turn all warnings into hard errors.
And there is an interesting project called rust-clippy which provides additional warnings as a compiler plugin.
I'd like to add that if you call rustc -W help it will show you a set of lints along with the default levels. You can set the levels using -W (for warn) or -D (for deny) followed by the lint name. The latter also applies to clippy (apart from help, but we have a pretty comprehensive wiki).