Don't you think this Option::<i32>::Some(32) syntax is a little bit inconsistant?

You can do both of the following:

let some_number=Option::<i32>::Some(32);
let absent_number : Option<i32> = None;

but don't you feel the syntax is more consistent if you can write it in this way:

let some_number=Option<i32>::Some(32);

:joy:Thanks.

That could be interpreted as:

let some_number = (Option < i32) > ::Some(32);

Although I'm not sure if ::Some(32) is valid, it would be pretty late to go back and reparse the first part, not to mention that some other expression could be valid, thus creating further inconsistency.

Mandatory link: rust/bastion-of-the-turbofish.rs at master · rust-lang/rust · GitHub

Finally, if you want consistency you can do this:

let absent_number: Option::<i32> = None;
7 Likes

:rofl:Thanks, your suggestion really works.
If you cannot make them more beautiful, you make them uglier. Nice :star_struck:

For the record, C++ has no turbofish equivalent to disambiguate template parameters from comparison operators, and even though it kind of works, it makes the already absurdly complicated parsing even more vexing – and you still need to disambiguate in certain situations with either the template or typename keyword. Rust is, at least, consistent.

It's a valid invocation of a function/tuple struct constructor that's named Some and exists in the crate root.

2 Likes

It's 100% valid; a leading double-colon qualifies the top-level namespace.

(But even if it weren't, this sort of ambiguity would still be bad.)

I'm pretty sure the accusatory "don't you think this is kind of stupid"-like pharsing, combined with a suggestive emoji, is not the appropriate way to ask a question. A bit more respect for the language designers, please. Rust is an exceptionally well-designed language, and things aren't ad-hoc or without a reason. This, rather than "oh it's a design/compiler bug", should be your default assumption.

8 Likes

I'd prefer

let some_number=Some(32_i32);

which require less code to wrote.

7 Likes

FWIW, I didn't take OP's tone and emoji use as being critical the language (on the contrary, I read ":joy:" as an attempt of lightening the question's tone), but that's the trouble with emoji, many of them are ambiguous and should be avoided outside very casual conversations.

5 Likes

That is true, there is none any disrespect here. Thanks.

2 Likes

More alternatives……

    let some_number = <Option<i32>>::Some(32);
    let some_number = Option::Some::<i32>(32);
    // and, since `Option::Some` is in the prelude:
    let some_number = Some::<i32>(32);

:slight_smile:

6 Likes

Well, if you want to, you can write it like this:

let some_number = <Option<i32>>::Some(32);

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6f14e450fda4072731eda339a9bc88ff

(This is "UFCS", though not in the form usually seen where it includes an as Trait.)

5 Likes