Rfcs: RFC: Add syntaxic sugar for struct expressions fields from variables of the same name

Created on 24 Sep 2014  Â·  10Comments  Â·  Source: rust-lang/rfcs

Issue by SimonSapin
_Sunday Feb 02, 2014 at 12:29 GMT_

_For earlier discussion, see https://github.com/rust-lang/rust/issues/11990_

_This issue was labelled with: A-parser, B-RFC in the Rust repository_


The general syntax for a struct pattern is:

StructName { field_name: new_variable_name }

But when the desired variable name is the same as the field name, it does not need to be repeated:

let MyStruct { name } = struct_value;
do_something_with(name);

This proposal is to add similar syntax sugar for struct expressions:

let name = …;
let struct_value = MyStruct { name };

… would be the same as:

let name = …;
let struct_value = MyStruct { name: name };

CC: @eddyb

All 10 comments

This was discussed in https://github.com/rust-lang/meeting-minutes/blob/master/weekly-meetings/2014-02-11.md#binding-in-struct-patterns

There were some concerns about ambiguities in e.g. for x in Foo { x } { }, but the grammar was changed since then (I think?) so that the desugared form of this would not be valid either.

fn main() {
    struct Foo { x: () }
    impl Iterator<()> for Foo {
        fn next(&mut self) -> Option<()> { None }
    }
    let x = ();
    for i in Foo { x: x } { }
}
a.rs:7:21: 7:22 error: expected one of `;`, `}`, found `:`
a.rs:7     for i in Foo { x: x } { }

CC @nick29581

Is this still unambiguous with type ascription?

@theemathas: it is still unambiguous; the full form of struct initialisation is defined appropriately so that type ascription works, and the additional, simpler form that this permits cannot have any colons (it’s commas and identifiers only), so there is no interaction with the type ascription feature.

So, this feature was suggested (https://github.com/rust-lang/rust/issues/11990) and then prototyped ( https://github.com/rust-lang/rust/pull/11994 ) back in February 2014

The team collectively declined to adopt the feature for Rust 1.0 : https://github.com/rust-lang/rust/issues/11990#issuecomment-34791865 (again, back in Febuary 2014).

But then in June 2014, we accepted RFC 92, that restricted the use of struct literals to only appear in _certain_ expression positions.

I believe that with the restriction specified by RFC 92, we could readily add this feature to the language backwards compatibly; that is, the parsing ambiguity issues are no longer present, I think.

I'm going to assign this to myself as a reminder to try to draft up a formal RFC for it at some point in the (hopefully near) future.

Sounds great, thanks for championing this Felix!

In feature we will have feature _Rename variable_ which will break code or should be insert field name.

@KalitaAlexey I infer you are talking about an IDE feature.

I think the language design should not hinder itself on how IDE features are to be implemented

@pnkfelix
Any updates? Do you still plan to write an RFC?
Since implementation is trivial, it's only a matter of decision.

@petrochenkov I've been distracted. Anyone else is free to draft up an RFC; I won't be upset if that happens.

This is implemented and stabilized in https://github.com/rust-lang/rust/pull/39761
ping @SimonSapin @pnkfelix for closing

Was this page helpful?
0 / 5 - 0 ratings