New Rust feature: Struct Field Initialization Shorthand

A new language feature has landed in nightly Rust 1.14 behind a feature gate (sometime between October 25 and November 1, 2016), called field_init_shorthand. This allows you to make struct expressions using the bindings in scope in a shorthand form. Essentially, you can name fields of a struct expression with the same name as variables. For example:

pub struct Test {  
  pub a: i32
}

fn make_it(a: i32) -> Test {  
  Test { a: a }
}

can now be expressed as:

#![feature(field_init_shorthand)]

pub struct Test {  
  pub a: i32
}

fn make_it(a: i32) -> Test {  
  Test { a }
}

Run this code

This is essentially a checked syntax desugaring; it's very small, but reasonably helpful. As it is behind a feature gate, it can only be used on nightly Rust, but given its simple semantics and that it doesn't overload the meaning of existing code, I don't expect it to take long to be stabilized.

Pull Request

Tracking issue

RFC