W3cubDocs

/Rust

Enumeration Variant expressions

Syntax
EnumerationVariantExpression :
EnumExprStruct
| EnumExprTuple
| EnumExprFieldless

EnumExprStruct :
PathInExpression { EnumExprFields? }

EnumExprFields :
EnumExprField (, EnumExprField)* ,?

EnumExprField :
IDENTIFIER
| (IDENTIFIER | TUPLE_INDEX) : Expression

EnumExprTuple :
PathInExpression (
( Expression (, Expression)* ,? )?
)

EnumExprFieldless : PathInExpression

Enumeration variants can be constructed similarly to structs, using a path to an enum variant instead of to a struct:

#![allow(unused)]
fn main() {
enum Message {
    Quit,
    WriteString(String),
    Move { x: i32, y: i32 },
}
let q = Message::Quit;
let w = Message::WriteString("Some string".to_string());
let m = Message::Move { x: 50, y: 200 };
}

Enum variant expressions have the same syntax, behavior, and restrictions as struct expressions, except they do not support base update with the .. syntax.

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/reference/expressions/enum-variant-expr.html