4 ms·
The example of Rust is one where no types are nullable by default, but only if you wrap them in an `Option<T>`. Given these two function signatures: pub f
by ubertaco 3y ago
The example of Rust is one where no types are nullable by default, but only if you wrap them in an `Option<T>`.
Given these two function signatures:
pub fn getUser() -> User
pub fn getMaybeUser() -> Option<User>
This code won't compile, because `getUser()` cannot return a `None`:
pub fn foo() -> Option<String> {
let user = getUser()?;
return user.name
}
But this code will compile:
pub fn foo2() -> Option<String> {
let user = getMaybeUser()?;
return Some(user.name)
}
(rust playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8e1e919c56b1f0945abede5113f94850 https://play.rust-lang.org/?version=stable&mode=debug&editio...)