Puzzle 4: Explanation
Let’s find out how counters and loops work in Rust.
Test it out
Hit “Run” to see the code’s output. The answer depends on how we run the program.
Running in debug mode
If we run the program in debug mode with cargo run
, the program will display a series of numbers from 0 to 127 and then crash while displaying the following error message:
thread 'main' panicked at 'attempt to add with overflow', main.rs:5:7.
Press + to interact
fn main() {let mut counter : i8 = 0;loop {println!("{}", counter);counter += 1;}}
Run the program in release mode
If we run the program in release mode with cargo run --release
, the program will display a series of numbers from 0 to 127 and then from −128 to −1. It will ...