Rust can have a piece of data that might have a value (Some
), or not (None
), with Option
.
Option
should NOT be used as a return value/type… but nothing except bad practices will stop you.
Instead of returning a
Option
use Result
Typically Option
is found when defining a structure of data…
struct User {
name: String,
email: Option<String>,
age: Option<u32>,
}
User above will have a name, but might not have email or age.
let u: User = User{
name: "Apollo".to_string(),
email: Some("[email protected]".to_string()),
age: None
};
The above shows how to assign something (Some
) to an Option
, and nothing (None
) to another Option
.
Check out my if let post to know how to access email and age