I am using the amazing anyhow
crate to propogate errors in a rust project I am working on.
Here is a code snippet:
pub fn parse_str(s: &str) -> Result<(String, Vec<String>)> {
let (key, operations) = s
.split_once(':')
.ok_or_else(|| anyhow!("invalid format: expected \"KEY:OP[+OP]*\", found \"{}\"", s))?;
let (key, operations) = match s.split_once(':') {
Some(split) => split,
None => bail!("invalid format: expected \"KEY:OP[+OP]*\", found \"{}\"", s),
};
Both of the "(key, operation)" code blocks work, my question is whether one of them is more of a best practice. Personally, I think the second snippet is much more explicit and easier to read, I just find the Some(split) => split
to be a little unnecessary and wanted to ask if there exists any syntax sugar in Rust to get rid of it or make it shorter somehow.
Thanks for your help!