# Pre-RFC: \`=\` for struct construction

**URL:** <https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470>\
**Category:** Uncategorized\
**Created:** [May 8, 2018, 1:21pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470 "2018-05-08T13:21:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 8, 2018, 1:21pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/1 "2018-05-08T13:21:26Z")

</div>

Edit: I’ll update this from time to time

# Summary

Introduction of `=` for struct construction in Rust 2018.

```rust
let my_struct = MyStruct { a = 42 }; // New
let my_struct = MyStruct { a: 42 }; // Current

```

The new syntax is intended to gradually replace the current syntax. Existing code will continue to compile without warnings.

# Motivation

Rust uses `=` in assignment expressions. In contrast to this, when creating an instance of a struct, the `:` instead of `=` is used to initialize struct fields with values. This proposal aims to introduce a consistent notation for assignment related operations by introducing `=` for struct field initialization.

As a consequence, this introduces a clear distinction in notation for assignment related operations and type annotations/definitions/ascriptions. Here are a few examples where `:` is used to define the type:

```rust
// Struct definition
struct User {
    username: String,
    email: String,
}

// Trait bounds
fn print_area<T: HasArea>(shape: T) {
    println!("This shape has an area of {}", shape.area());
}

```

Instead the use of `=` introduces similarity to variable bindings:

```rust
let my_value = 42; // Assignment
let my_value: u64 = 42 // Assignment with type annotation
let my_struct = MyStruct { a = 42 }; // New struct construction syntax using `=`

```

This author believes that the improved consistency helps beginners. Furthermore, the notational difference between the definition and construction of structs is valuable to all users of Rust because with it, it becomes possible to tell them apart at a glance.

# Guide-level explanation

## Shipment strategy

**Before Rust 2018 is shipped**

- Introduce support for the `=` syntax
- Continue support of `:` syntax without any warnings
- Update to rustfix to make easy migration possible
- Update the documentation for Rust 2018 to use the new syntax
- Add a rule to clippy, disabled at first

**After Rust 2018 has shipped**

- Continue support of `:` syntax without any warnings
- Enable clippy rule gradually (different clippy profiles)

**Before next edition after Rust 2018**

- New RFC that discusses the deprecation of the old syntax if this is desired

## Destructuring and matching

Occurrences of `:` are replaced with `=`, the rest of the syntax remains unchanged:

```rust
struct MyStruct { x: i32 }
...

let my_struct = MyStruct { x: 42 }; // Current
let my_struct = MyStruct { x = 42 }; // New

// Destructuring: x_var = 42
let MyStruct { x: x_var } = my_struct; // Current
let MyStruct { x = x_var } = my_struct; // New
let x_var = my_struct.x // Without destructuring

```

```rust
// Matching
match Foo { x = 9 } {
    // x
    Foo { x } => {} // Current
    Foo { x } => {} // New
    
    // x_var
    Foo { x: x_var} => {} // Current
    Foo { x = x_var} => {} // New

    // No var
    Foo { x: 1..=10 } => {} // Current
    Foo { x = 1..=10 } => {} // New

    // x_var
    Foo { x: x_var @ 1..=10} => {} // Current
    Foo { x = x_var @ 1..=10 } => {} // New
}

```

## Type ascriptions

This change makes struct construction fully compatible with type ascription in expressions (See [RFC803](https://github.com/rust-lang/rfcs/blob/master/text/0803-type-ascription.md))

```rust
let my_struct = MyStruct { a = 42: u64 };

```

# Reference-level explanation

_ToDo_

# Drawbacks

- Requires many documentation changes
- Different syntax than other languages: The current syntax mirrors the syntax used in JavaScript. The proposed change would make Rust’s syntax different. It is however similar to the syntax of C99 (explained further below)
- Struct definition and initialization now look different. Some people might consider this as a drawback. This author thinks that it is actually advantageous.

# Rationale and alternatives

- Keep the current syntax, or
- Ship the `=` syntax with a different strategy than what is proposed here

# Prior art

## Similar syntax in C99

C (and therefore C++ as well) uses almost the same syntax to what is proposed here with the difference that a `.` is placed in front of each field name.

```rust
struct point p = { .y = yvalue, .x = xvalue };

```

[Source](https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html)

## Previous RFC and discussions

- [Previous RFC (RFC65) prior to Rust 1.0](https://github.com/rust-lang/rfcs/pull/65) for the same idea (not merged)
- [Recent discussion (March 2018) in users forum](https://users.rust-lang.org/t/noob-question-about-data-struct-ure-syntax/16140)

# Unresolved questions

n/a

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 8, 2018, 1:29pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/2 "2018-05-08T13:29:19Z")

</div>

> Prior art: Similar syntax in C99

You specifically [asserted in the other thread](https://internals.rust-lang.org/t/pre-rfc-disallow-using-assignment-in-a-function-call-in-rust-2018-disallow-f-a-b/7469/13) that "just because a language uses the syntax doesn't mean that it makes sense". Therefore I find it puzzling that you think "C does this" is a meaningful argument in favor of a **wildly** breaking change.

> [@MajorBreakfast](#):
>
> This proposal aims to introduce a consistent notation for assignment

It's not exactly _assigment_ though, or not necessarily. You _can_ think of it as assignment, but the notion of a "key-value pair" or "name-value pair" is equally valid, in which case it's not better, it's worse than the status quo. Anyway, such a relatively minor difference in interpretation of a construct doesn't warrant, again, a **massively painful** change to a **very fundamental** part of the language. I think this is a _very_ bad idea.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [May 8, 2018, 1:33pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/3 "2018-05-08T13:33:18Z")

</div>

This was previously proposed as [RFC #65](https://github.com/rust-lang/rfcs/pull/65).

It was also recently discussed [in the users forum](https://users.rust-lang.org/t/noob-question-about-data-struct-ure-syntax/16140).

---

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 8, 2018, 1:37pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/4 "2018-05-08T13:37:58Z")

</div>

> [@H2CO3](#):
>
> You specifically asserted in the other thread that “just because a language uses the syntax doesn’t mean that it makes sense”. Therefore I find it puzzling that you think “C does this” is a meaningful argument in favor of a wildly breaking change.

I did. And I think that it's more important to have consistency than to do something another programming language does. Nevertheless it is relevant for the discussion to mention that C99 has a similar syntax.

Also, this proposal won't break anything. Instead it proposes continued support of the current syntax.

> [@H2CO3](#):
>
> It’s not exactly assigment though, or not necessarily. You can think of it as assignment, but the notion of a “key-value pair” or “name-value pair” is equally valid

Can you elaborate what you mean with "It’s not exactly assigment"? It think it assigns values to struct fields.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 8, 2018, 1:42pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/5 "2018-05-08T13:42:55Z")

</div>

In procedural speak, you might say “assign”. It’s not _changing_ anything though, so I would actually prefer the term “initializes the field” or “binds the value to the field”. Struct construction is not really about mutating a value, but about constructing a new one from parts.

(I’m not sure what the official terminology for this is, though.)

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [May 8, 2018, 1:46pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/6 "2018-05-08T13:46:06Z")

</div>

> [@MajorBreakfast](#):
>
> fn print\_area\<T: HasArea\>

Nit: the `:` syntax is not used to "define the type" here (I know you didn't mean this, but it could be textually a bit clearer) 😉

I find the main advantage of this proposal to be the freeing up of the type ascription syntax from the troubles it has been in. I think the proposed syntax is certainly more internally consistent than the status quo. Using the judgemental form `x : τ` for initializing/updating/binding is deeply strange to me.

That said; Nearly every crate will be touched by this, so this step should only be taken with _much care and deliberation_.

PS: I think your plan of action (_"shipment strategy"_) is reasonable _proviso that we want to take this step_.

---

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 8, 2018, 1:50pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/7 "2018-05-08T13:50:53Z")

</div>

> [@Centril](#):
>
> Nit: the : syntax is not used to “define the type” here

Someone will need to go over this and correct all the terminology blunders. I'm also pretty sure that the assignment expressions I give as an example are actually just the definitions of a bindings. The compiler guys will probably be aghast and say "these differences should be clear to everyone!" ^^' I just know how to use it

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 8, 2018, 1:53pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/8 "2018-05-08T13:53:03Z")

</div>

> [@MajorBreakfast](#):
>
> This change makes struct construction fully compatible with type ascription in expressions

That should already work with the current syntax though, shouldn't it? Isn't `let s = MyStruct { a: 42: u64 };` already unambiguous to parse? Field name, colon, then an expression, where the expression can contain an optional type ascription.

And while we are at consistency — another problem with this proposal is this: will it change struct patterns too? If so, will it change them to `MyStruct { name = value }` as well? Because that doesn't make sense at all:

```rust
match foo {
    Foo { name = 1337 } => …,
}

```

This looks like the field is being assigned the value 1337 in the pattern, whereas it's not, it's a comparison. That's downright confusing.

---

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 8, 2018, 1:57pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/9 "2018-05-08T13:57:05Z")

</div>

Type ascriptions should work with the current syntax too, but it doesn't look very tidy. I continue to think that type ascriptions aren't useful because the struct has already the types defined. I just wanted to mention how it would look like.

> [@H2CO3](#):
>
> will it change struct patterns too?

I've just looked at the old RFC and saw that too. Yes IMO it should. It has to be added to the proposal.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 8, 2018, 1:58pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/10 "2018-05-08T13:58:09Z")

</div>

Well, that’s even worse (for the reasons I enlisted). That’s not even a subjective consideration, making patterns look like assignments is objectively bad, it’s totally backwards.

---

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 8, 2018, 2:01pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/11 "2018-05-08T14:01:02Z")

</div>

Destructuring is IMO precisely assignment backwards. ^^’

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 8, 2018, 2:02pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/12 "2018-05-08T14:02:41Z")

</div>

It’s not though, if you look at the example I posted. Nothing is being assigned here, the struct is not even being constructed. Instead, the `name` field is being compared against a value. It’s misleading to denote it as an assignment of the value to the field.

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [May 8, 2018, 2:02pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/13 "2018-05-08T14:02:54Z")

</div>

> [@MajorBreakfast](#):
>
> let my\_struct = MyStruct { a = 42: u64 };

If you want to use this particular example, the reader is right to ask: _"what about `42u64`?"_. A different example might show your point better.

> [@MajorBreakfast](#):
>
> Type ascriptions should work with the current syntax too, but it doesn’t look very tidy.

Unfortunately, no. Given the form:

```rust
fn main() {
    struct MyStruct { a: u64 }
    let MyStruct { a: foobar } = MyStruct { a: 1 };
}

```

I would suspect that this is making the typing judgement `a : foobar`.

> [@H2CO3](#):
>
> That’s not even a subjective consideration, making patterns look like assignments is objectively bad.

I don't know about _"objectively"_; but my view is at least that it would be quite confusing / misleading. That said, the current _rebind-as_ syntax is also terribly confusing (with typing judgements).

A possible non-confusing syntax would be:

```rust
fn main() {
    struct MyStruct { a: u64 }
    let MyStruct { a as foobar } = MyStruct { a: 1 };
}

```

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 8, 2018, 2:05pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/14 "2018-05-08T14:05:32Z")

</div>

To be honest, I don’t see how the `as` keyword, which is currently only associated with type casting, is better in this sense. It would now gain an overloaded meaning of destructuring, compared to `:` which is already used for two different purposes in two different contexts. (It wouldn’t be different vice versa either, but that’s history.)

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [May 8, 2018, 2:09pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/15 "2018-05-08T14:09:05Z")

</div>

Regardless of whether `=` or `:` is the better syntax, this change implies such a tremendously huge amount of churn that I consider it a complete non-starter.

For comparison, when I wrote [the dyn trait RFC](https://github.com/rust-lang/rfcs/pull/2113), I always felt that was just about the maximum amount of churn we could possibly get away with, and hopefully the churniest change we’re ever going to do after 1.0. And that was a case where the existing syntax was so deeply confusing that people often had no idea they were operating on trait objects rather than traits. Even if you don’t like today’s struct syntax, I don’t think anyone’s found `:`s so confusing that _they failed to understand they were initializing a struct_.

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [May 8, 2018, 2:10pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/16 "2018-05-08T14:10:34Z")

</div>

> [@H2CO3](#):
>
> To be honest, I don’t see how the as keyword, which is currently only associated with type casting, is better in this sense.

Darn it! Why is everything so jumbled and why can't we have nice things... 😢

I wanted it to evoke association with:

```rust
use path::to:thing as foobar;

```

... but I see the possible confusing reading you pointed out.

Well; if we want to go "fully unambiguous" _(not saying we should)_ a syntax _could be_:

```rust
let MyStruct { use a as foobar } = MyStruct { a: 1 };

```

Another syntax comes to mind..:

```rust
let MyStruct { a @ foobar } = MyStruct { a: 1 };

```

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [May 8, 2018, 2:15pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/17 "2018-05-08T14:15:10Z")

</div>

Oh, and indeed, `as` already has another meaning, in imports. In this case that argument of mine doesn’t stand as strong.

(I don’t think we should try making this particular case “fully unambiguous” either, btw – the status quo is pretty clear, but granted, it’s certainly possible to come up with something completely non-overloaded. If this _had_ to be changed, though, I would lean towards `@` since it is already familiar as a top-level pattern binder, so it would have a similar function, and it wouldn’t introduce lengthy keywords in a so commonly-used syntactic construct.)

---

<div class="post-metadata">

**Author:** ![MajorBreakfast](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/majorbreakfast/32/4015_2.png) [@MajorBreakfast](https://internals.rust-lang.org/u/MajorBreakfast)\
**Post date:** [May 8, 2018, 2:15pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/18 "2018-05-08T14:15:40Z")

</div>

The current destructuring syntax isn’t very intuitive. But yes, if we changed it to use `=`, like my proposal has intended (didn’t explicitly say so, but it was the intention), it’d make it even worse. Maybe @Centril is right that it should be more verbose with a nice litte keyword that says what’s what? I can say from experience that the destructuring syntax with `:` can definitely be also very confusing.

---

<div class="post-metadata">

**Author:** ![mpol](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mpol/32/4056_2.png) [@mpol](https://internals.rust-lang.org/u/mpol)\
**Post date:** [May 8, 2018, 2:17pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/19 "2018-05-08T14:17:03Z")

</div>

For prior art we also have Haskell, which _does_ use `=` in record pattern matching. See eg. [https://en.wikibooks.org/wiki/Haskell/Pattern\_matching#Introduction\_to\_records](https://en.wikibooks.org/wiki/Haskell/Pattern_matching#Introduction_to_records)

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [May 8, 2018, 2:23pm UTC](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470/20 "2018-05-08T14:23:47Z")

</div>

> [@mpol](#):
>
> For prior art we also have Haskell, which does use = in record pattern matching. See eg. [Haskell/Pattern matching - Wikibooks, open books for an open world](https://en.wikibooks.org/wiki/Haskell/Pattern_matching#Introduction_to_records)

Right! I've clearly not written enough Haskell lately; it is showing...

> [@H2CO3](#):
>
> the status quo is pretty clear

Respectfully I disagree, I find any use of the type-judgemental form to be confusing when it is actually not doing typing judgments.

Perhaps the `Struct { field: binding }` form is not common enough that it won't be as bad as bricking the `field: stuff` syntax everywhere, so it's a much smaller delta. It might be worth a crater run to see how much would break and then introduce `Struct { field @ binding }` which is a much more consistent and natural syntax and then use the other form for type ascription eventually.

[Next page](https://internals.rust-lang.org/t/pre-rfc-for-struct-construction/7470.md?page=2)
