3 ms·
What I mean is that if you don't have exceptions and `return` returns only from a given block, it takes some serious exercise to break out of a "fn ... { ...eac
by viraptor 14y ago
What I mean is that if you don't have exceptions and `return` returns only from a given block, it takes some serious exercise to break out of a "fn ... { ...each { ... match ... } } }".
Return will only return from one of them, so to signal an error from the match block, you need to:
- save the result in a mutable variable in a function
- do something that results in "false" returned from each block
- return the result from the function (probably casting mut->immut on the way)
What I'd really like is either a stack unwinding exception, or a `return` keyword that's different from the "last expression in a block" syntax. Last expression could give a result for a closure, match, if, etc. while `return` could always break out of a function. For example (pseudo-code, proper syntax doesn't matter here):
fn parse_into_list(s: &str) -> Option<Stuff> {
let res = do s.map |c| {
match c {
bad_character => return None
...
}
}
Some(Stuff(res))
}
Instead of:
fn parse_into_list(s: &str) -> Option<Stuff> {
let mut result: ...;
let mut correct: true;
do s.each |c| {
match c {
bad_character => {correct = false; false}
good_character => { do_stuff_with_result; true}
}
}
if correct {
Some(Stuff(result))
} else {
None
}
}