I proposed properties before, but was ultimately convinced that they wouldn't be very useful in Rust, because struct fields can't be replaced with properties in a backwards-compatible way.
All proposals in this thread are flawed, since they doesn't work across crates. A better syntax would be
use some_crate::Foo::{self, self.color as colour};
let foo: Foo = ..;
assert_eq!(foo.color, foo.colour);
Note that self.color
can be a field or a method. If a field and a method both have the name color
, both are renamed.
This would even enable us to convert a tuple struct to a struct with named fields:
// this works today:
struct Foo(u8, u8, u8);
let _ = Foo { 0: 255, 1: 0, 2: 0 };
// if we could rename fields, we could write this:
use Foo.{0 as red, 1 as green, 2 as blue};
let _ = Foo { red: 255, green: 0, blue: 0 };
However, I'm still not convinced that this actually needed. Language features need a strong motivation; for example, type aliases are used to make type declarations smaller and less generic, and imported items can be renamed mainly to prevent name clashes when importing multiple items of the same name.