One thing Rust doesn't seem to be doing very well yet is guard clauses, specifically when handling Option . I've seen and appreciated the use of guard clauses in many languages, as a good way to quickly check for a few conditions at the top of a function, and return early if those conditions aren't met. Since it seems that Option are recommended in Rust, there's a lot of time you want to quickly return if `Some(x)` i…
You can use ? on an Option if your type returns an Option. If it returns a Result, you can use ok_or()?, and at some point in the nearish future, you can just use ?.
My current understanding is that those would return an Error only? I was more describing cases where you do want to return, but not necessarily return an `Error`.
For instance in a simplified example function that returns a boolean, you could decide to return `false`. is it possible there?
// Function that returns a boolean value
fn is_equal_to_ten(n: Option) -> bool {
// some one liner that checks for None, if it's not none, gives you `x` when `n` matches content of `Some(x)` (not real code):
if let Some(x) = n else { return false; /* what to do in case it's a None*/ }
// `x` is available here:
return (x == 10);
}
Would this be considered bad practice in Rust?