November 2017
Intermediate to advanced
264 pages
5h 45m
English
An obvious way to use variables is to print out their value, as is done here:
// see Chapter 2/code/constants2.rs
static MAX_HEALTH: i32 = 100;
static GAME_NAME: &str = "Monster Attack";
const MYPI: f32 = 3.14;
fn main() {
println!("The Game you are playing is called {}.", GAME_NAME);
println!("You start with {} health points.", MAX_HEALTH);
}
This gives an output which looks like this:
The Game you are playing is called Monster Attack.
You start with 100 health points.
The first argument of the println! macro is a literal format string containing a placeholder {}. The value of the constant or variable after the comma is converted to a string and comes in its place. There can be more than one placeholder ...
Read now
Unlock full access