Constants are valid for the entire time a program runs, within the scope in which they were declared.
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
Rust is a statically typed language
compiler can usually infer what type we want to use based on the value and how we use it.
In cases when many types are possible, such as when we converted a String to a numeric type using parse in [02-guessing-game], we must add a type annotation
let guess: u32 = "42".parse().expect("Not a number!");debug builds check for it
release builds wrap around
for explicit checking see wrapping_*, checked_*, overflowing_*, saturating_* families of methods
U+0000 to U+D7FF and U+E000 to U+10FFFF inclusivelet tup: (i32, f64, u8) = (500, 6.4, 1);
let tup = (500, 6.4, 1);
let (x, y, z) = tup;
println!("The value of y is: {y}");
let x: (i32, f64, u8) = (500, 6.4, 1);
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
looplet result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
If you have loops within loops, break and continue apply to the innermost loop at that point.
You can optionally specify a loop label on a loop that you can then use with break or continue
Loop labels must begin with a single quote. Here’s an example with two nested loops:
break that doesn’t specify a label will exit the inner loop only.break 'counting_up; statement will exit the outer loopfn main() {
let mut count = 0;
'counting_up: loop {
println!("count = {count}");
let mut remaining = 10;
loop {
println!("remaining = {remaining}");
if remaining == 9 {
break;
}
if count == 2 {
break 'counting_up;
}
remaining -= 1;
}
count += 1;
}
println!("End count = {count}");
}
continue is encountered, the current iteration is terminated, returning control to the loop head, typically continuing with the next iteration.whilewhile number != 0 {
println!("{number}!");
number -= 1;
}
forlet a = [10, 20, 30, 40, 50];
for element in a {
println!("the value is: {element}");
}
for number in (1..4).rev() {
println!("{number}!");
}