#[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)
A slice literal expression: [a, b, c, d]
.
Assign(ExprAssign)
An assignment expression: a = compute()
.
AssignOp(ExprAssignOp)
A compound assignment expression: counter += 1
.
Async(ExprAsync)
An async block: async { ... }
.
Await(ExprAwait)
An await expression: fut.await
.
Binary(ExprBinary)
A binary operation: a + b
, a * b
.
Block(ExprBlock)
A blocked scope: { ... }
.
Box(ExprBox)
A box expression: box f
.
Break(ExprBreak)
A break
, with an optional label to break and an optional
expression.
Call(ExprCall)
A function call expression: invoke(a, b)
.
Cast(ExprCast)
A cast expression: foo as f64
.
Closure(ExprClosure)
A closure expression: |a, b| a + b
.
Continue(ExprContinue)
A continue
, with an optional label.
Field(ExprField)
Access of a named struct field (obj.k
) or unnamed tuple struct
field (obj.0
).
ForLoop(ExprForLoop)
A for loop: for pat in expr { ... }
.
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
.
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.
Index(ExprIndex)
A square bracketed indexing expression: vector[2]
.
Let(ExprLet)
A let
guard: let Some(x) = opt
.
Lit(ExprLit)
A literal in place of an expression: 1
, "foo"
.
Loop(ExprLoop)
Conditionless loop: loop { ... }
.
Macro(ExprMacro)
A macro invocation expression: format!("{}", q)
.
Match(ExprMatch)
A match
expression: match n { Some(n) => {}, None => {} }
.
MethodCall(ExprMethodCall)
A method call expression: x.foo::<T>(a, b)
.
Paren(ExprParen)
A parenthesized expression: (a + b)
.
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.
Range(ExprRange)
A range expression: 1..2
, 1..
, ..2
, 1..=2
, ..=2
.
Reference(ExprReference)
A referencing operation: &a
or &mut a
.
Repeat(ExprRepeat)
An array literal constructed from one repeated element: [0u8; N]
.
Return(ExprReturn)
A return
, with an optional value to be returned.
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 }
.
Try(ExprTry)
A try-expression: expr?
.
TryBlock(ExprTryBlock)
A try block: try { ... }
.
Tuple(ExprTuple)
A tuple expression: (a, b, c, d)
.
Type(ExprType)
A type ascription expression: foo: f64
.
Unary(ExprUnary)
A unary operation: !x
, *x
.
Unsafe(ExprUnsafe)
An unsafe block: unsafe { ... }
.
Verbatim(TokenStream)
Tokens in expression position not interpreted by Syn.
While(ExprWhile)
A while loop: while expr { ... }
.
Yield(ExprYield)
A yield expression: yield expr
.
Trait Implementations
sourceimpl From<ExprAssign> for Expr
impl From<ExprAssign> for Expr
sourcefn from(e: ExprAssign) -> Expr
fn from(e: ExprAssign) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprAssignOp> for Expr
impl From<ExprAssignOp> for Expr
sourcefn from(e: ExprAssignOp) -> Expr
fn from(e: ExprAssignOp) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprBinary> for Expr
impl From<ExprBinary> for Expr
sourcefn from(e: ExprBinary) -> Expr
fn from(e: ExprBinary) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprClosure> for Expr
impl From<ExprClosure> for Expr
sourcefn from(e: ExprClosure) -> Expr
fn from(e: ExprClosure) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprContinue> for Expr
impl From<ExprContinue> for Expr
sourcefn from(e: ExprContinue) -> Expr
fn from(e: ExprContinue) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprForLoop> for Expr
impl From<ExprForLoop> for Expr
sourcefn from(e: ExprForLoop) -> Expr
fn from(e: ExprForLoop) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprMethodCall> for Expr
impl From<ExprMethodCall> for Expr
sourcefn from(e: ExprMethodCall) -> Expr
fn from(e: ExprMethodCall) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprReference> for Expr
impl From<ExprReference> for Expr
sourcefn from(e: ExprReference) -> Expr
fn from(e: ExprReference) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprRepeat> for Expr
impl From<ExprRepeat> for Expr
sourcefn from(e: ExprRepeat) -> Expr
fn from(e: ExprRepeat) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprReturn> for Expr
impl From<ExprReturn> for Expr
sourcefn from(e: ExprReturn) -> Expr
fn from(e: ExprReturn) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprStruct> for Expr
impl From<ExprStruct> for Expr
sourcefn from(e: ExprStruct) -> Expr
fn from(e: ExprStruct) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprTryBlock> for Expr
impl From<ExprTryBlock> for Expr
sourcefn from(e: ExprTryBlock) -> Expr
fn from(e: ExprTryBlock) -> Expr
Converts to this type from the input type.
sourceimpl From<ExprUnsafe> for Expr
impl From<ExprUnsafe> for Expr
sourcefn from(e: ExprUnsafe) -> Expr
fn from(e: ExprUnsafe) -> Expr
Converts to this type from the input type.
Auto Trait Implementations
impl RefUnwindSafe for Expr
impl !Send for Expr
impl !Sync for Expr
impl Unpin for Expr
impl UnwindSafe for Expr
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more