Allow reserved words after dot and colon-colon

Is there a proposal for allowing this (based on EcmaScript):

struct C {
    r#struct: f64,
}

fn main() {
    let o = C { r#struct: 0.0 };
    let _v = o.struct;
}

_v should be set to o.r#struct. (No use-case yet.)

Although I said there's no use-case, this can allow more readable user constructs such as:

o.for(|a| 0);

Rather than

o.r#for(|a| 0);

Following in the footsteps of postfix .await, there’s been quite a few discussions of allowing other keywords (like match) to be used in postfix position. So I don’t think now would be a good time to blanket allow this for all keywords.

16 Likes

In addition to this, I think allowing keywords in this position would look like a language construct, leaving people wondering what it did.

I don't think we'd want to support this, precisely because people will not assume it's an ordinary field/method.

2 Likes

But if you come to think of it, EcmaScript allows you to do promise.catch(...) and promise.finally(...).

Maybe it looks confusing because of the current Rust syntax highlighter.

ECMAScript doesn't have any keywords that come after the dot operator so there's no possibility for confusion. Even if Rust never adds more keywords like await, it would still potentially be confusing that await is treated differently from other keywords

3 Likes

I heard some people argue against x.await Rust's syntax. But since it's there already, I agree it can be inconsistent to allow reserved words after dot.

At least it can work after ::.

There are at least some proposals involving the use of :: with a keyword. For instance, if you have a function xyz, then xyz::return might be its return type, and xyz::fn might be its whole function type.

(Not commenting on whether those are especially likely to happen, just that they're an argument for having some available space reserved for future functionality.)

10 Likes

There is an existing keyword used after :: too, self in use std::vec::self;.

7 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.