.hg .zn In #Rust, you can make `foo(); return Ok(())` more terse as `return Ok(foo())`, assuming `<typeof foo as FnOnce>::Output == ()`.

This is especially nice on the expression side of a match branch, because it saves you having to make a block.

But is it good?

(post-poll-close responses welcome in the replies)

.hg We don't have a concrete example for this. I'm a little worried that it'll be too confusing to be worth the conciseness, at least in some cases.

@moonbolt I'd agree with this being too confusing, at a glance it looks as if the result of the function somehow depends on the result of `foo`, even though it cannot.

@timorl @moonbolt If it semantically depends on `foo`, then I think Ok(foo()) is fine. Example:

fn calc(a: i8, b: i8, c: &mut i32, op: Op) -> Result<(), ZeroDivision> {
match op {
Op::Add => Ok(add(a, b, c)),
Op::Sub => Ok(sub(a, b, c)),
Op::Mul => {
*c = (a as i32) * (b as i32);
Ok(())
},
Op::Div => if b == 0 {
Err(ZeroDivision)
} else {
Ok(div(a, b, c))
}
}
}

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.