Enum syn::Expr [−][src]
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),
// some variants omitted
}
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
Array(ExprArray)
A slice literal expression: [a, b, c, d]
.
Tuple Fields of Array
0: ExprArray
Assign(ExprAssign)
An assignment expression: a = compute()
.
Tuple Fields of Assign
0: ExprAssign
AssignOp(ExprAssignOp)
A compound assignment expression: counter += 1
.
Tuple Fields of AssignOp
0: ExprAssignOp
Async(ExprAsync)
An async block: async { ... }
.
Tuple Fields of Async
0: ExprAsync
Await(ExprAwait)
An await expression: fut.await
.
Tuple Fields of Await
0: ExprAwait
Binary(ExprBinary)
A binary operation: a + b
, a * b
.
Tuple Fields of Binary
0: ExprBinary
Block(ExprBlock)
A blocked scope: { ... }
.
Tuple Fields of Block
0: ExprBlock
Box(ExprBox)
A box expression: box f
.
Tuple Fields of Box
0: ExprBox
Break(ExprBreak)
A break
, with an optional label to break and an optional
expression.
Tuple Fields of Break
0: ExprBreak
Call(ExprCall)
A function call expression: invoke(a, b)
.
Tuple Fields of Call
0: ExprCall
Cast(ExprCast)
A cast expression: foo as f64
.
Tuple Fields of Cast
0: ExprCast
Closure(ExprClosure)
A closure expression: |a, b| a + b
.
Tuple Fields of Closure
0: ExprClosure
Continue(ExprContinue)
A continue
, with an optional label.
Tuple Fields of Continue
0: ExprContinue
Field(ExprField)
Access of a named struct field (obj.k
) or unnamed tuple struct
field (obj.0
).
Tuple Fields of Field
0: ExprField
ForLoop(ExprForLoop)
A for loop: for pat in expr { ... }
.
Tuple Fields of ForLoop
0: ExprForLoop
Group(ExprGroup)
An 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
.
Tuple Fields of Group
0: ExprGroup
If(ExprIf)
An 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.
Tuple Fields of If
0: ExprIf
Index(ExprIndex)
A square bracketed indexing expression: vector[2]
.
Tuple Fields of Index
0: ExprIndex
Let(ExprLet)
A let
guard: let Some(x) = opt
.
Tuple Fields of Let
0: ExprLet
Lit(ExprLit)
A literal in place of an expression: 1
, "foo"
.
Tuple Fields of Lit
0: ExprLit
Loop(ExprLoop)
Conditionless loop: loop { ... }
.
Tuple Fields of Loop
0: ExprLoop
Macro(ExprMacro)
A macro invocation expression: format!("{}", q)
.
Tuple Fields of Macro
0: ExprMacro
Match(ExprMatch)
A match
expression: match n { Some(n) => {}, None => {} }
.
Tuple Fields of Match
0: ExprMatch
MethodCall(ExprMethodCall)
A method call expression: x.foo::<T>(a, b)
.
Tuple Fields of MethodCall
Paren(ExprParen)
A parenthesized expression: (a + b)
.
Tuple Fields of Paren
0: ExprParen
Path(ExprPath)
A 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.
Tuple Fields of Path
0: ExprPath
Range(ExprRange)
A range expression: 1..2
, 1..
, ..2
, 1..=2
, ..=2
.
Tuple Fields of Range
0: ExprRange
Reference(ExprReference)
A referencing operation: &a
or &mut a
.
Tuple Fields of Reference
Repeat(ExprRepeat)
An array literal constructed from one repeated element: [0u8; N]
.
Tuple Fields of Repeat
0: ExprRepeat
Return(ExprReturn)
A return
, with an optional value to be returned.
Tuple Fields of Return
0: ExprReturn
Struct(ExprStruct)
A 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 }
.
Tuple Fields of Struct
0: ExprStruct
Try(ExprTry)
A try-expression: expr?
.
Tuple Fields of Try
0: ExprTry
TryBlock(ExprTryBlock)
A try block: try { ... }
.
Tuple Fields of TryBlock
0: ExprTryBlock
Tuple(ExprTuple)
A tuple expression: (a, b, c, d)
.
Tuple Fields of Tuple
0: ExprTuple
Type(ExprType)
A type ascription expression: foo: f64
.
Tuple Fields of Type
0: ExprType
Unary(ExprUnary)
A unary operation: !x
, *x
.
Tuple Fields of Unary
0: ExprUnary
Unsafe(ExprUnsafe)
An unsafe block: unsafe { ... }
.
Tuple Fields of Unsafe
0: ExprUnsafe
Verbatim(TokenStream)
Tokens in expression position not interpreted by Syn.
Tuple Fields of Verbatim
0: TokenStream
While(ExprWhile)
A while loop: while expr { ... }
.
Tuple Fields of While
0: ExprWhile
Yield(ExprYield)
A yield expression: yield expr
.
Tuple Fields of Yield
0: ExprYield
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