O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Constants and variables

Rust deviates from the mainstream here by making constants the default variable type. If you need a variable that can be mutated, you use the let mut keyword:

// variables.rs fn main() {   let mut target = "world";   println!("Howdy, {}", target);   target = "mate";   println!("Howdy, {}", target); } 

Conditionals should also look familiar; they follow the C-like if...else pattern. Since Rust is strongly-typed, the condition must be a Boolean type:

// conditionals.rs fn main() {   let condition = true;   if condition {     println!("Condition was true");   } else {     println!("Condition was false");   } } 

In Rust, if is not a statement but an expression. This distinction means that if always returns a value. The value may be ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required