Color theme
0 day streak0 XPP1
RustLearn mode
MISSION 1 OF 1

Read an optional bearing

0 XP earned
Rust track: MasterMission: AdvancedOptionPattern matchingMissing values
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.

x
0 / 2 challenges
LEARN THE CONCEPT

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.

main.rs
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.