Read an optional bearing
Before you start this master track
Typed functions and collections. Be ready for ownership, moves, mutability, and enum matching.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
A compass reading may be unavailable. Option represents that possibility explicitly: Some holds a bearing, and None represents no reading. match examines which case is present and creates the appropriate message. Both arms return a String, so the result has one consistent type without inventing a numeric reading for missing data.
fn main() { let bearing: Option<i32> = None; let message = match bearing { Some(degrees) => format!("Heading {}", degrees), None => String::from("No signal"), }; println!("{}", message);}Each mission is a complete independent Rust program using the standard library. Save as main.rs, compile with rustc main.rs, then run the resulting executable. Rust 2021 edition or later is suitable.
Track the values line by line. Predict the result before running this example in your own editor.