Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntactic sugar for unwrap() #3769

Closed
npetrangelo opened this issue Feb 8, 2025 · 2 comments
Closed

Syntactic sugar for unwrap() #3769

npetrangelo opened this issue Feb 8, 2025 · 2 comments

Comments

@npetrangelo
Copy link

I propose to use the ! character to represent unwrap() on Result and Option types, similar to ? being used for Err and None propagation.

So this:

fn foo(bar: Option<Bar>) -> Bar {
    bar.unwrap()
}

becomes this:

fn foo(bar: Option<Bar>) -> Bar {
    bar!
}

Please do not respond with some flavor of "but syntax proposals are always bad" or "this is not strictly necessary." That is categorically unhelpful. Keep your judgements to the proposed syntax itself.

One issue with this could be if the Option or Result is of a function type, and unwrapping it to call it makes it resemble a macro.

Any other thoughts?

@programmerjake
Copy link
Member

there have been proposals for exactly this syntax before: https://internals.rust-lang.org/t/unwrap-operator/18306/11

some people brought up concerns that we don't want to encourage just calling unwrap everywhere, so it shouldn't have special syntax.

@kennytm
Copy link
Member

kennytm commented Feb 8, 2025

One issue with this could be if the Option or Result is of a function type, and unwrapping it to call it makes it resemble a macro.

not just function type.

let vec = Some(vec ! [1, 2, 3]); // what does `!` mean here?
let item = vec ! [0]; // what does `!` mean here?
// let item = vec.unwrap()[0];
dbg!(item);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants