Are nested matches a bad practice in idiomatic Rust?

The easiest way to make things a bit cleaner is to drop some braces:

match client.get(url).send() {
    Ok(mut res) =>
        match res.read_to_string(&mut s) {
            Ok(_) => Some(s),
            Err(_) => None,
        },
    Err(_) => None,
}

The inner match can be expressed a little cleaner perhaps as

match client.get(url).send() {
    Ok(mut res) =>
        res.read_to_string(&mut s).ok().map(|_| s),

    Err(_) => None,
}

This suggests using a map on the outer type (to get Result<Option<_>, _>) and then dropping the result with .unwrap_or(None) or .unwrap_or_default()

client.get(url).send()
      .map(|mut res| res.read_to_string(&mut s).ok().map(|_| s))
      .unwrap_or(None)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)