https://www.recurse.com/manual
Rust doesn't have a concept of "null".
However, Rust has an enum that can encode the concept of a value being present or absent. This enum is Option<T>
enum Option<T> { Some(T), None, }
<T> means the Some variant of the Option enum can hold one piece of data of any type.
fn get_full_name( firstName: &str lastName: &str ) -> String { ... } fn get_full_name_with_middle( firstName: &str, middleName: &str, lastName: &str ) -> String { ... }
...
fn get_full_name( firstName: &str, middleName: Option<&str>, lastName: &str
,
) -> String {
...
}
get_full_name("Leela", None, "Turanga")
Result<T, E>
Ok(T)
Err(E)
fn get_status(username: &str) -> Result<&str, String> { // some user lookup code here... if username != "janedoe" { return Err("Not found".to_string()); } // if user exists, fetch their status Ok("User is active") } fn main () { let result = get_status("janedoe"); match result { Ok(status) => println!("{}", status), Err(e) => println!("{}", e), } }
Rustlings
https://github.com/rust-lang/rustlings
Rustlings
$ rustlings run option1
$ rustlings hint option1
Complete Exercises option 1, 2 error_handling