Curriculum Data modeling Option instead of null
Option instead of null
Rust does not have null; absence is Option<T>. TypeScript users already know T | null; Rust makes the check mandatory before access. Planned exercises: 1. choose Option<T>. 2. recognise Some and None. 3. choose match over unchecked access. 4. fill Some(value). 5. fill unwrap_or. 6. write a function returning Option. 7. write if let Some. 8. translate TS nullable code. 9. fix a program by handling the None case explicitly.
Exercises
9 ready
01
pick one
TypeScript spells "a string that might be absent" as string | null.
02
pick one
Given let n: Option<i32> = Some(7);, what value (of the same
03
pick one
Pick the strongest reason to handle Option<T> with match
04
fill blanks
Fill the variant name that constructs an Option<i32> carrying
05
fill blanks
Fill the method that extracts the inner value or substitutes a
06
write a program
Write divide(a: i32, b: i32) -> Option<i32> that returns
07
write a program
Write show(n: Option<i32>) that prints v=<value> when the
08
write a program
Translate the TS nullable-returning function. find_user(id)
09
write a program
This program reaches for .unwrap() and panics on the None