Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Assignee expressions can contain rest-patterns.

Aperta
#2,208 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Documentazione
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
documentation

Direzione di ricerca

Inizia con le sezioni della Rust Reference sulle assignee expressions e sulle struct expressions, quindi confronta i casi di tuple, slice e call-expression descritti qui. Leggi il materiale collegato su default field values e riproduci gli esempi forniti. Il lavoro è completato quando la discrepanza tra compilatore e Reference è stata risolta e viene documentata una regola non ambigua con esempi appropriati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

See also https://github.com/rust-lang/reference/issues/2209 for more on assignee expressions.

The following two code snippets compile:

struct Thing { _field: i32 }

fn main() {
    Thing { .. } = Thing { _field: 1 };
}
macro_rules! foo {
    ($x:expr) => {}
}

foo!(Thing { .. });

This implies that Thing { .. } is an expression. In particular, since it's valid on the lhs of an assignment expression, Thing { .. } must be an assignee expression.

An assignee expression can be a struct expression. However, struct expressions cannot contain rest-patterns. Therefore, according to the reference, Thing { .. } cannot be an assignee expression.

Thus, either the compiler or the reference is wrong. I think the reference should be somehow amended to match the compiler.

Similar issues affect rest-patterns in tuples ((x, y, ..)), in slices ([x, y, ..]), and in call expressions (tuple structs; Thing(x, y, ..)).


To make things worse, as pointed out by @kpreid, the Thing { .. } syntax is also currently legal in nightly as syntax for default field values. For example, the following code compiles:

#![feature(default_field_values)]
struct Foo {
    x: i32 = 0,
}
fn main() {
    // creates a struct with default field values, then assigns it to a rest-pattern
    Foo { .. } = Foo { .. };
    // creates two structs with default field values, then assigns one to another
    *&mut Foo { .. } = Foo { .. };
}

I don't know how to specify a disambiguation rule between default field values vs rest-patterns in assignee expressions.

Lingua principale
Rust
Stelle
1.6k
Fork
609
Merge medio
1g 4h
PR unite (30g)
7

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/reference

Tutte le issue di rust-lang/reference

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.