Enum syn::Expr [−][src]
#[non_exhaustive]
pub enum Expr {
Show 40 variants
Array(ExprArray),
Assign(ExprAssign),
AssignOp(ExprAssignOp),
Async(ExprAsync),
Await(ExprAwait),
Binary(ExprBinary),
Block(ExprBlock),
Box(ExprBox),
Break(ExprBreak),
Call(ExprCall),
Cast(ExprCast),
Closure(ExprClosure),
Continue(ExprContinue),
Field(ExprField),
ForLoop(ExprForLoop),
Group(ExprGroup),
If(ExprIf),
Index(ExprIndex),
Let(ExprLet),
Lit(ExprLit),
Loop(ExprLoop),
Macro(ExprMacro),
Match(ExprMatch),
MethodCall(ExprMethodCall),
Paren(ExprParen),
Path(ExprPath),
Range(ExprRange),
Reference(ExprReference),
Repeat(ExprRepeat),
Return(ExprReturn),
Struct(ExprStruct),
Try(ExprTry),
TryBlock(ExprTryBlock),
Tuple(ExprTuple),
Type(ExprType),
Unary(ExprUnary),
Unsafe(ExprUnsafe),
Verbatim(TokenStream),
While(ExprWhile),
Yield(ExprYield),
}Expand description
A Rust expression.
This type is available only if Syn is built with the "derive" or "full"
feature, but most of the variants are not available unless “full” is enabled.
Syntax tree enums
This type is a syntax tree enum. In Syn this and other syntax tree enums are designed to be traversed using the following rebinding idiom.
let expr: Expr = /* ... */;
match expr {
Expr::MethodCall(expr) => {
/* ... */
}
Expr::Cast(expr) => {
/* ... */
}
Expr::If(expr) => {
/* ... */
}
/* ... */We begin with a variable expr of type Expr that has no fields
(because it is an enum), and by matching on it and rebinding a variable
with the same name expr we effectively imbue our variable with all of
the data fields provided by the variant that it turned out to be. So for
example above if we ended up in the MethodCall case then we get to use
expr.receiver, expr.args etc; if we ended up in the If case we get
to use expr.cond, expr.then_branch, expr.else_branch.
This approach avoids repeating the variant names twice on every line.
// Repetitive; recommend not doing this.
match expr {
Expr::MethodCall(ExprMethodCall { method, args, .. }) => {In general, the name to which a syntax tree enum variant is bound should be a suitable name for the complete syntax tree enum type.
// Binding is called `base` which is the name I would use if I were
// assigning `*discriminant.base` without an `if let`.
if let Expr::Tuple(base) = *discriminant.base {A sign that you may not be choosing the right variable names is if you
see names getting repeated in your code, like accessing
receiver.receiver or pat.pat or cond.cond.
Variants (Non-exhaustive)
This enum is marked as non-exhaustive
Array(ExprArray)
Tuple Fields
0: ExprArrayA slice literal expression: [a, b, c, d].
Assign(ExprAssign)
Tuple Fields
0: ExprAssignAn assignment expression: a = compute().
AssignOp(ExprAssignOp)
Tuple Fields
0: ExprAssignOpA compound assignment expression: counter += 1.
Async(ExprAsync)
Tuple Fields
0: ExprAsyncAn async block: async { ... }.
Await(ExprAwait)
Tuple Fields
0: ExprAwaitAn await expression: fut.await.
Binary(ExprBinary)
Tuple Fields
0: ExprBinaryA binary operation: a + b, a * b.
Block(ExprBlock)
Tuple Fields
0: ExprBlockA blocked scope: { ... }.
Box(ExprBox)
Tuple Fields
0: ExprBoxA box expression: box f.
Break(ExprBreak)
Tuple Fields
0: ExprBreakA break, with an optional label to break and an optional
expression.
Call(ExprCall)
Tuple Fields
0: ExprCallA function call expression: invoke(a, b).
Cast(ExprCast)
Tuple Fields
0: ExprCastA cast expression: foo as f64.
Closure(ExprClosure)
Tuple Fields
0: ExprClosureA closure expression: |a, b| a + b.
Continue(ExprContinue)
Tuple Fields
0: ExprContinueA continue, with an optional label.
Field(ExprField)
Tuple Fields
0: ExprFieldAccess of a named struct field (obj.k) or unnamed tuple struct
field (obj.0).
ForLoop(ExprForLoop)
Tuple Fields
0: ExprForLoopA for loop: for pat in expr { ... }.
Group(ExprGroup)
Tuple Fields
0: ExprGroupAn expression contained within invisible delimiters.
This variant is important for faithfully representing the precedence
of expressions and is related to None-delimited spans in a
TokenStream.
If(ExprIf)
Tuple Fields
0: ExprIfAn if expression with an optional else block: if expr { ... } else { ... }.
The else branch expression may only be an If or Block
expression, not any of the other types of expression.
Index(ExprIndex)
Tuple Fields
0: ExprIndexA square bracketed indexing expression: vector[2].
Let(ExprLet)
Tuple Fields
0: ExprLetA let guard: let Some(x) = opt.
Lit(ExprLit)
Tuple Fields
0: ExprLitA literal in place of an expression: 1, "foo".
Loop(ExprLoop)
Tuple Fields
0: ExprLoopConditionless loop: loop { ... }.
Macro(ExprMacro)
Tuple Fields
0: ExprMacroA macro invocation expression: format!("{}", q).
Match(ExprMatch)
Tuple Fields
0: ExprMatchA match expression: match n { Some(n) => {}, None => {} }.
MethodCall(ExprMethodCall)
Tuple Fields
A method call expression: x.foo::<T>(a, b).
Paren(ExprParen)
Tuple Fields
0: ExprParenA parenthesized expression: (a + b).
Path(ExprPath)
Tuple Fields
0: ExprPathA path like std::mem::replace possibly containing generic
parameters and a qualified self-type.
A plain identifier like x is a path of length 1.
Range(ExprRange)
Tuple Fields
0: ExprRangeA range expression: 1..2, 1.., ..2, 1..=2, ..=2.
Reference(ExprReference)
Tuple Fields
A referencing operation: &a or &mut a.
Repeat(ExprRepeat)
Tuple Fields
0: ExprRepeatAn array literal constructed from one repeated element: [0u8; N].
Return(ExprReturn)
Tuple Fields
0: ExprReturnA return, with an optional value to be returned.
Struct(ExprStruct)
Tuple Fields
0: ExprStructA struct literal expression: Point { x: 1, y: 1 }.
The rest provides the value of the remaining fields as in S { a: 1, b: 1, ..rest }.
Try(ExprTry)
Tuple Fields
0: ExprTryA try-expression: expr?.
TryBlock(ExprTryBlock)
Tuple Fields
0: ExprTryBlockA try block: try { ... }.
Tuple(ExprTuple)
Tuple Fields
0: ExprTupleA tuple expression: (a, b, c, d).
Type(ExprType)
Tuple Fields
0: ExprTypeA type ascription expression: foo: f64.
Unary(ExprUnary)
Tuple Fields
0: ExprUnaryA unary operation: !x, *x.
Unsafe(ExprUnsafe)
Tuple Fields
0: ExprUnsafeAn unsafe block: unsafe { ... }.
Verbatim(TokenStream)
Tuple Fields
0: TokenStreamTokens in expression position not interpreted by Syn.
While(ExprWhile)
Tuple Fields
0: ExprWhileA while loop: while expr { ... }.
Yield(ExprYield)
Tuple Fields
0: ExprYieldA yield expression: yield expr.
Implementations
An alternative to the primary Expr::parse parser (from the
Parse trait) for ambiguous syntactic positions in which a
trailing brace should not be taken as part of the expression.
Rust grammar has an ambiguity where braces sometimes turn a path
expression into a struct initialization and sometimes do not. In the
following code, the expression S {} is one expression. Presumably
there is an empty struct struct S {} defined somewhere which it is
instantiating.
let _ = *S {};
// parsed by rustc as: `*(S {})`We would want to parse the above using Expr::parse after the =
token.
But in the following, S {} is not a struct init expression.
if *S {} {}
// parsed by rustc as:
//
// if (*S) {
// /* empty block */
// }
// {
// /* another empty block */
// }For that reason we would want to parse if-conditions using
Expr::parse_without_eager_brace after the if token. Same for
similar syntactic positions such as the condition expr after a
while token or the expr at the top of a match.
The Rust grammar’s choices around which way this ambiguity is resolved at various syntactic positions is fairly arbitrary. Really either parse behavior could work in most positions, and language designers just decide each case based on which is more likely to be what the programmer had in mind most of the time.
if return S {} {}
// parsed by rustc as:
//
// if (return (S {})) {
// }
//
// but could equally well have been this other arbitrary choice:
//
// if (return S) {
// }
// {}Note the grammar ambiguity on trailing braces is distinct from
precedence and is not captured by assigning a precedence level to
the braced struct init expr in relation to other operators. This can
be illustrated by return 0..S {} vs match 0..S {}. The former
parses as return (0..(S {})) implying tighter precedence for
struct init than .., while the latter parses as match (0..S) {}
implying tighter precedence for .. than struct init, a
contradiction.
Trait Implementations
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Performs the conversion.
Auto Trait Implementations
impl RefUnwindSafe for Expr
impl UnwindSafe for Expr
Blanket Implementations
Mutably borrows from an owned value. Read more
