I'm probably being really dumb here as I am such a newbie to Rust that I'm still only on Chapter 5 of "the book" but here goes anyway.
I was just reading an article picking holes in the 'safety' claims made for V and idly wondered What form Rust's complaining about similar errors would take. So I tried a couple of the examples in that post and, to my surprise, cargo check
didn't see anything wrong with them...
INTEGER OVERFLOW
fn main() {
//INTEGER OVERLOW
let mut x:i32 = 2147483647;
x += 1;
println!("Overflow? --> {}",x);
}
cargo check
doesn't complain...
Checking tester v0.1.0 (/Users/stuzbot/claraithe/rust/tester)
Finished dev [unoptimized + debuginfo] target(s) in 0.14s
but, as expected, cargo run
does...
~/claraithe/rust/tester á
cargo run
Compiling tester v0.1.0 (/Users/stuzbot/claraithe/rust/tester)
error: this arithmetic operation will overflow
--> src/main.rs:4:1
|
4 | x += 1;
| ^^^^^^ attempt to compute `i32::MAX + 1_i32`, which would overflow
DIVIDE BY ZERO
//DIVIDE BY ZERO
fn main() {
let x = 42;
let y = 0;
let z = x/y;
println!("Z ==> {}",z);
}
Again, cargo check
doesn't complain...
~/claraithe/rust/tester á
cargo check
Checking tester v0.1.0 (/Users/stuzbot/claraithe/rust/tester)
Finished dev [unoptimized + debuginfo] target(s) in 0.72s
But, as expected, cargo run does:
~/claraithe/rust/tester á
cargo run
Compiling tester v0.1.0 (/Users/stuzbot/claraithe/rust/tester)
error: this operation will panic at runtime
--> src/main.rs:6:9
|
6 | let z = x/y;
| ^^^ attempt to divide `42_i32` by zero
INo doubt I'm missing the distinctions between cargo check
and cargo run
but the impression I got from what I've read of the Rust Programming Book so far was that cargo check
was generally used to run a quick check on your code while working on it,, to make sure it compiles OK --without the overhead of actually building each time
Cargo also provides a command called
cargo check
. This command quickly checks your code to make sure it compiles but doesnât produce an executable:
So, why is cargo check
not flagging up these two very obvious errors? Shouldn't they be spotted at compile time? I don't see how either could only be caught at runtime.
EDIT: substituted 'cargo check' for 'cargo run' in last paragraph.