I want to unwrap the cookie or return an empty &str
when is None
:
let cookie: Option<Cookie> = req.cookie("timezone");
// right, but foolish:
let timezone: String = match cookie {
Some(t) => t.value().to_string(),
None => "".into(),
};
This is an error:
let timezone = cookie.unwrap_or("").value();
You can use unwrap_or_default
plus map
, what you want is to extract a String
value, and if it cannot be done just have a default one. Order matters:
let timezone: String = cookie.map(|c| c.value().to_string()).unwrap_or_default();