Skip to content

Idea: Built-in way to go from bool to Option #2606

@ElectricCoffee

Description

@ElectricCoffee

The idea is pretty simple: Add a function called some to booleans such that you can automatically convert to an option.

The basic implementation wouldn't be much more than

fn some<T>(self, data: T) -> Option<T> {
  if self {
    Some(data)
  } else {
    None
  }
}

The purpose of this would be to be able to write something like regex.is_match(string).some(token) without having to explicitly set up an if-statement.

Maybe also add in an *_or_else style function that takes a closure as an input, such that the true case is only ever allocated if it succeeds: regex.is_match(string).and_some(|| build_token()).

Yes/No/Maybe?
I'd like to hear your opinions :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions