November 2017
Intermediate to advanced
264 pages
5h 45m
English
By now, we know how to use functions, like in the following example where function triples changes our strength variable, but only if the return value of the function triples is assigned to the variable strength:
// see code in Chapter 5/code/higher_functions.rs
let mut strength = 26;
println!("My tripled strength equals {}",triples(strength)); // 78
println!("My strength is still {}", strength); // 26
strength = triples(strength);
println!("My strength is now {}", strength); // 78
Here, the function triples is defined as the following function:
fn triples(s: i32) -> i32 { 3 * s }
In the preceding code, s represents a value for the strength variable.
Suppose our player smashes an amazing power stone, ...
Read now
Unlock full access