# Input: Gauging feelings about syntax

**URL:** <https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797>\
**Category:** language design\
**Created:** [May 10, 2024, 3:09am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797 "2024-05-10T03:09:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoshBashed](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshbashed/32/10905_2.png) [@JoshBashed](https://internals.rust-lang.org/u/JoshBashed)\
**Post date:** [May 10, 2024, 3:09am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/1 "2024-05-10T03:09:18Z")

</div>

Hi there. I am gauging feedback for what syntax I might propose as part of my implied enums RFC. Feel free to leave feedback. [Inferred types `_::Enum` by JoshuaBrest · Pull Request #3444 · rust-lang/rfcs · GitHub](https://github.com/rust-lang/rfcs/pull/3444#issuecomment-2103563122)

---

<div class="post-metadata">

**Author:** ![idanarye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/idanarye/32/3346_2.png) [@idanarye](https://internals.rust-lang.org/u/idanarye)\
**Post date:** [May 10, 2024, 10:51pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/2 "2024-05-10T22:51:32Z")

</div>

Enums in Swift already use `.` (`Foo.bar`), so the "Swift-like" version in Rust would be `::Bar` rather than `.Bar`. The fact that this syntax is already taken does not mean we should start mixing up the namespacing syntax...

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [May 10, 2024, 11:11pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/3 "2024-05-10T23:11:56Z")

</div>

I know this topic has come up a lot here before. Personally I'm of the opinion that `_` should work in more places regardless of whether or not another even briefer syntax exists for enums.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [May 10, 2024, 11:21pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/4 "2024-05-10T23:21:58Z")

</div>

I still worry that `_::Foo` working suggests that `_::replace(&mut a, 0)` should work too, or other things like `_::into_raw(x)`.

So when I think it's intentional that those _won't_ work with this, I like using something that doesn't give those implications, and it being shorter and less annoying to type is a nice bonus.

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [May 11, 2024, 12:13am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/5 "2024-05-11T00:13:17Z")

</div>

> [@scottmcm](#):
>
> \_::replace(&mut a, 0)

I actually would like that to work! Okay to be clear, I don't want that to work. I want you to have to `use std::mem` and explicitly say `mem::replace`, but `_::replace` should mostly compile with an error that says those things instead of

```rust
   Compiling playground v0.0.1 (/playground)
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `::`
 --> src/main.rs:3:14
  |
3 | _::replace(&mut a, 12);
  | ^^ expected one of `.`, `;`, `?`, `else`, or an operator

error: could not compile `playground` (bin "playground") due to 1 previous error

```

I think this kind of functionality is mostly implemented in IDE/rustanalyzer right now, but it seems like the compiler could assume `mem`, warn me about the unused must-use value, and only error at the very end of compilation with a helpful message about what I need to be explicit about.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jrose/32/9591_2.png) [@jrose](https://internals.rust-lang.org/u/jrose)\
**Post date:** [May 11, 2024, 12:36am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/6 "2024-05-11T00:36:08Z")

</div>

I think this is a false comparison, `_::Foo` doesn’t work either unless there’s _context_ that suggests the base type in Rust’s existing inference logic. It’s like using `Bar::<_>::Baz` in a pattern. I agree it’s a bit verbose though.

---

<div class="post-metadata">

**Author:** ![JoshBashed](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/joshbashed/32/10905_2.png) [@JoshBashed](https://internals.rust-lang.org/u/JoshBashed)\
**Post date:** [May 11, 2024, 12:50am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/7 "2024-05-11T00:50:55Z")

</div>

@idanarye @toc @scottmcm @jrose I will not be responding to any comments here. I recommend continuing the discussion in the GitHub PR.

---

<div class="post-metadata">

**Author:** ![idanarye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/idanarye/32/3346_2.png) [@idanarye](https://internals.rust-lang.org/u/idanarye)\
**Post date:** [May 11, 2024, 1:07am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/8 "2024-05-11T01:07:12Z")

</div>

`_::replace(&mut a, 0)` should not work for the same reason `_(&mut a, 0)` cannot work.

---

<div class="post-metadata">

**Author:** ![phaylon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/phaylon/32/62_2.png) [@phaylon](https://internals.rust-lang.org/u/phaylon)\
**Post date:** [May 11, 2024, 7:38am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/9 "2024-05-11T07:38:27Z")

</div>

> [@scottmcm](#):
>
> I still worry that `_::Foo` working suggests that `_::replace(&mut a, 0)` should work too, or other things like `_::into_raw(x)`.

I would argue that is the case because while `_::replace(&mut a, 0)` doesn't work, our brains already know what it's supposed to _mean_.

To me that is a big win in the clarity column.

---

<div class="post-metadata">

**Author:** ![8573](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/8573/32/4913_2.png) [@8573](https://internals.rust-lang.org/u/8573)\
**Post date:** [May 11, 2024, 6:15pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/10 "2024-05-11T18:15:59Z")

</div>

> [@phaylon](#):
>
> our brains already know what it's supposed to _mean_.

Do they? I didn't realize it meant specifically `std::mem::replace` until the following post mentioned `std::mem`. Any crate can have a function named "replace", and one might mean any one of those.

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://internals.rust-lang.org/u/2e71828)\
**Post date:** [May 11, 2024, 7:04pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/11 "2024-05-11T19:04:17Z")

</div>

> [@8573](#):
>
> Any crate can have a function named "replace", and one might mean any one of those.

Personally, I assumed the `_` would be replaced by some type which has an associated function or method called `replace` with a matching signature. The idea of `_` representing a module name is quite surprising.

---

<div class="post-metadata">

**Author:** ![idanarye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/idanarye/32/3346_2.png) [@idanarye](https://internals.rust-lang.org/u/idanarye)\
**Post date:** [May 11, 2024, 9:20pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/12 "2024-05-11T21:20:09Z")

</div>

> [@2e71828](#):
>
> Personally, I assumed the `_` would be replaced by some type which has an associated function or method called `replace` with a matching signature.

That, too, is a bad idea because multiple types may have a `replace` function with the same signature. This kind of works with traits - like the infamous `<_>::default()`. But that's because even without type inference, trait functions face the same issue. That is - `Foo::default()` already needs to infer that what we want to call is `<Foo as Default>::default()` (and not being able to infer that would be too much of a loss), so `<_>::default()` can (and should!) be thought of as inferring `Default::default()` rather than `Foo::default()`.

I think type inference should stick to what I'm going to dub "_The IRS Rule_": if the compiler already knows what the type should be, and will punish us (with error messages) if we put any other type there, then why can't it just infer that type for us?

But if there is no such unique type (that's unique by principle, not by coincidence) then there should be no type inference.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [May 12, 2024, 1:02am UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/13 "2024-05-12T01:02:41Z")

</div>

I think this would be a good addition to the language.

There has been a concern raised previously that the _current_ code assumes the variants are used along the enum name, so you have:

```rust
fetch(url, Cookies::Enabled)

```

and the new syntax makes it less clear:

```rust
fetch(url, _::Enabled)

```

whereas if Rust supported `_::` from the start, people would use a different naming scheme:

```rust
fetch(url, _::WithCookies)

```

so for that I suggest paring the proposal with some kind of "alias" feature:

```rust
enum Cookies {
    #[alias=WithCookies]
    Enabled,
    #[alias=CookieMonsterSad]
    Disabled,
}

```

---

<div class="post-metadata">

**Author:** ![idanarye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/idanarye/32/3346_2.png) [@idanarye](https://internals.rust-lang.org/u/idanarye)\
**Post date:** [May 12, 2024, 12:58pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/14 "2024-05-12T12:58:50Z")

</div>

If you try to run Clippy on an enum that encodes its purpose inside the variants' names [it'll yell at you](https://rust-lang.github.io/rust-clippy/master/index.html#/enum_variant_names):

> **[Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=00eb406d1a26bf646a4d817224a39052)**
>
> A browser interface to the Rust compiler to experiment with the language

But I think the `#[alias]` thing is redundant. If we are going to trust programmers to use the alias and write `_::WithCookies` instead of `_::Enabled`, why not just trust them to write `Cookies::Enabled`? (even if it does mean they have to import the enum)

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [May 12, 2024, 2:05pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/15 "2024-05-12T14:05:11Z")

</div>

Agreed. I think a better approach would be a clippy lint that warms when using the shorthand for unclear variant names.

But I don't think it's a big issue at all. First off all, is just `Enabled` really that bad? I don't consider it any worse than `true`. Also in many editors, you can see the parameter name hinted before the argument value anyways:

```rust
fetch(url, cookie_behavior: _::Enabled)

```

---

<div class="post-metadata">

**Author:** ![idanarye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/idanarye/32/3346_2.png) [@idanarye](https://internals.rust-lang.org/u/idanarye)\
**Post date:** [May 12, 2024, 2:35pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/16 "2024-05-12T14:35:03Z")

</div>

> [@pitaj](#):
>
> I don't consider it any worse than `true`.

Yes, but the entire purpose of using the `Cookies` enum is the notion that a boolean is not clear enough.

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [May 12, 2024, 4:37pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/17 "2024-05-12T16:37:32Z")

</div>

I would personally only use a unit enum like that if there are more than two variants or if it's commonly used alongside other binary arguments (that is there would be multiple bool arguments otherwise).

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [May 12, 2024, 5:24pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/18 "2024-05-12T17:24:43Z")

</div>

I believe the idea is the same as that of newtypes, to make sure you can't mix up the boolean for user accepted cookies with the boolean for being logged in as an admin (as an example). So there is a valid use for using strong types for this.

It also makes helps reduce confusion when it is not obvious which alternative should map to true and which should map to false. (Can't think of an example of the top of my head, but this has happened to me.)

And it is more extensible, let's say I have a bool `is_diesel_engine`. Then someone invents the electric car and now there are 3 options (petrol, diesel, electric). An enum is easier to extend (especially once fuel cell cars also showed up on the scene).

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [May 12, 2024, 6:05pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/19 "2024-05-12T18:05:32Z")

</div>

> [@Vorpal](#):
>
> believe the idea is the same as that of newtypes, to make sure you can't mix up the boolean for user accepted cookies with the boolean for being logged in as an admin (as an example). So there is a valid use for using strong types for this.

That would be under my "multiple binary arguments" point.

> [@Vorpal](#):
>
> And it is more extensible, let's say I have a bool `is_diesel_engine`. Then someone invents the electric car and now there are 3 options (petrol, diesel, electric). An enum is easier to extend (especially once fuel cell cars also showed up on the scene).

That would fall under my "more than two options" point.

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [May 12, 2024, 6:18pm UTC](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797/20 "2024-05-12T18:18:20Z")

</div>

> [@idanarye](#):
>
> if the compiler already knows what the type should be, and will punish us (with error messages) if we put any other type there, then why can't it just infer that type for us?

Because there already exist complains about how long the compiler takes to do its job and adding all of this searching on top would only exacerbate the problem.

[Next page](https://internals.rust-lang.org/t/input-gauging-feelings-about-syntax/20797.md?page=2)
