Killing `Ok(())` with `ok!()`

With wrapping try fns your snippet could look like this:

try fn foo(s: &[u8], what: &'static str) -> MyResult<String> {
    ffi::CStr::from_bytes_until_nul(s)
        .map_err(|e| MyError::NotCString(e, what))?
        .to_str()
        .map_err(|e| MyError::NotUTF8(e, what))?
        .to_string()
}

I think it's cleaner than your hypothetical .Ok().

It's a bit off-topic, but with a helper library like snafu you can improve it a bit further:

try fn foo(s: &[u8], what: &'static str) -> MyResult<String> {
    ffi::CStr::from_bytes_until_nul(s)
        .context(NotCStringSnafu{ what })?
        .to_str()
        .context(NotUTF8Snafu { what })?
        .to_string()
}
2 Likes