Rfcs: `if` with no pure `else` assigns as `Option`

Created on 3 Nov 2017  路  5Comments  路  Source: rust-lang/rfcs

Example:

let x = if test { // Option<i32>
    1
}

let y = if test { // Option<i32>
    1
} else if test2 {
    2
}

let z = if test { // i32
    1
} else {
    2
}
T-lang

Most helpful comment

The confusion caused by this, and harm to readability, far outweigh the 20 characters it saves : Some(..) else { None }

All 5 comments

The confusion caused by this, and harm to readability, far outweigh the 20 characters it saves : Some(..) else { None }

The problem is that let x = if test { 1 } is never compiled

There's a boolinator crate for situations like this one. Why not use it?

extern crate boolinator;
use boolinator::Boolinator;

let x = test.as_some_from(|| 1);

let y = test.as_some_from(|| 1).or_else(|| test2.as_some_from(|| 2));

This seems like a breaking change I don't think there is sufficient motivation to make with the edition mechanism so I'll close as non-actionable.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

silversolver1 picture silversolver1  路  3Comments

3442853561 picture 3442853561  路  3Comments

3442853561 picture 3442853561  路  4Comments

mahkoh picture mahkoh  路  3Comments

steveklabnik picture steveklabnik  路  4Comments