Enum Expr

Source
pub enum Expr {
Show 40 variants Box(ExprBox), InPlace(ExprInPlace), Array(ExprArray), Call(ExprCall), MethodCall(ExprMethodCall), Tuple(ExprTuple), Binary(ExprBinary), Unary(ExprUnary), Lit(ExprLit), Cast(ExprCast), Type(ExprType), If(ExprIf), IfLet(ExprIfLet), While(ExprWhile), WhileLet(ExprWhileLet), ForLoop(ExprForLoop), Loop(ExprLoop), Match(ExprMatch), Closure(ExprClosure), Unsafe(ExprUnsafe), Block(ExprBlock), Assign(ExprAssign), AssignOp(ExprAssignOp), Field(ExprField), Index(ExprIndex), Range(ExprRange), Path(ExprPath), AddrOf(ExprAddrOf), Break(ExprBreak), Continue(ExprContinue), Return(ExprReturn), Macro(ExprMacro), Struct(ExprStruct), Repeat(ExprRepeat), Paren(ExprParen), Group(ExprGroup), Try(ExprTry), Catch(ExprCatch), Yield(ExprYield), Verbatim(ExprVerbatim),
}
Expand description

A Rust expression.

This type is available if Syn is built with the "derive" or "full" feature.

§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::IfLet(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 IfLet case we get to use expr.pat, expr.then_branch, expr.else_branch.

The pattern is similar if the input expression is borrowed:

match *expr {
    Expr::MethodCall(ref expr) => {

This approach avoids repeating the variant names twice on every line.

Expr::MethodCall(ExprMethodCall { method, args, .. }) => { // repetitive

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(ref 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§

§

Box(ExprBox)

A box expression: box f.

This type is available if Syn is built with the "full" feature.

§

InPlace(ExprInPlace)

A placement expression: place <- value.

This type is available if Syn is built with the "full" feature.

§

Array(ExprArray)

A slice literal expression: [a, b, c, d].

This type is available if Syn is built with the "full" feature.

§

Call(ExprCall)

A function call expression: invoke(a, b).

This type is available if Syn is built with the "derive" or "full" feature.

§

MethodCall(ExprMethodCall)

A method call expression: x.foo::<T>(a, b).

This type is available if Syn is built with the "full" feature.

§

Tuple(ExprTuple)

A tuple expression: (a, b, c, d).

This type is available if Syn is built with the "full" feature.

§

Binary(ExprBinary)

A binary operation: a + b, a * b.

This type is available if Syn is built with the "derive" or "full" feature.

§

Unary(ExprUnary)

A unary operation: !x, *x.

This type is available if Syn is built with the "derive" or "full" feature.

§

Lit(ExprLit)

A literal in place of an expression: 1, "foo".

This type is available if Syn is built with the "derive" or "full" feature.

§

Cast(ExprCast)

A cast expression: foo as f64.

This type is available if Syn is built with the "derive" or "full" feature.

§

Type(ExprType)

A type ascription expression: foo: f64.

This type is available if Syn is built with the "full" feature.

§

If(ExprIf)

An if expression with an optional else block: if expr { ... } else { ... }.

The else branch expression may only be an If, IfLet, or Block expression, not any of the other types of expression.

This type is available if Syn is built with the "full" feature.

§

IfLet(ExprIfLet)

An if let expression with an optional else block: if let pat = expr { ... } else { ... }.

The else branch expression may only be an If, IfLet, or Block expression, not any of the other types of expression.

This type is available if Syn is built with the "full" feature.

§

While(ExprWhile)

A while loop: while expr { ... }.

This type is available if Syn is built with the "full" feature.

§

WhileLet(ExprWhileLet)

A while-let loop: while let pat = expr { ... }.

This type is available if Syn is built with the "full" feature.

§

ForLoop(ExprForLoop)

A for loop: for pat in expr { ... }.

This type is available if Syn is built with the "full" feature.

§

Loop(ExprLoop)

Conditionless loop: loop { ... }.

This type is available if Syn is built with the "full" feature.

§

Match(ExprMatch)

A match expression: match n { Some(n) => {}, None => {} }.

This type is available if Syn is built with the "full" feature.

§

Closure(ExprClosure)

A closure expression: |a, b| a + b.

This type is available if Syn is built with the "full" feature.

§

Unsafe(ExprUnsafe)

An unsafe block: unsafe { ... }.

This type is available if Syn is built with the "full" feature.

§

Block(ExprBlock)

A blocked scope: { ... }.

This type is available if Syn is built with the "full" feature.

§

Assign(ExprAssign)

An assignment expression: a = compute().

This type is available if Syn is built with the "full" feature.

§

AssignOp(ExprAssignOp)

A compound assignment expression: counter += 1.

This type is available if Syn is built with the "full" feature.

§

Field(ExprField)

Access of a named struct field (obj.k) or unnamed tuple struct field (obj.0).

This type is available if Syn is built with the "full" feature.

§

Index(ExprIndex)

A square bracketed indexing expression: vector[2].

This type is available if Syn is built with the "derive" or "full" feature.

§

Range(ExprRange)

A range expression: 1..2, 1.., ..2, 1..=2, ..=2.

This type is available if Syn is built with the "full" feature.

§

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.

This type is available if Syn is built with the "derive" or "full" feature.

§

AddrOf(ExprAddrOf)

A referencing operation: &a or &mut a.

This type is available if Syn is built with the "full" feature.

§

Break(ExprBreak)

A break, with an optional label to break and an optional expression.

This type is available if Syn is built with the "full" feature.

§

Continue(ExprContinue)

A continue, with an optional label.

This type is available if Syn is built with the "full" feature.

§

Return(ExprReturn)

A return, with an optional value to be returned.

This type is available if Syn is built with the "full" feature.

§

Macro(ExprMacro)

A macro invocation expression: format!("{}", q).

This type is available if Syn is built with the "full" feature.

§

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 }.

This type is available if Syn is built with the "full" feature.

§

Repeat(ExprRepeat)

An array literal constructed from one repeated element: [0u8; N].

This type is available if Syn is built with the "full" feature.

§

Paren(ExprParen)

A parenthesized expression: (a + b).

This type is available if Syn is built with the "full" feature.

§

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.

This type is available if Syn is built with the "full" feature.

§

Try(ExprTry)

A try-expression: expr?.

This type is available if Syn is built with the "full" feature.

§

Catch(ExprCatch)

A catch expression: do catch { ... }.

This type is available if Syn is built with the "full" feature.

§

Yield(ExprYield)

A yield expression: yield expr.

This type is available if Syn is built with the "full" feature.

§

Verbatim(ExprVerbatim)

Tokens in expression position not interpreted by Syn.

This type is available if Syn is built with the "derive" or "full" feature.

Trait Implementations§

Source§

impl Clone for Expr

Source§

fn clone(&self) -> Expr

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<ExprAddrOf> for Expr

Source§

fn from(e: ExprAddrOf) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprArray> for Expr

Source§

fn from(e: ExprArray) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprAssign> for Expr

Source§

fn from(e: ExprAssign) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprAssignOp> for Expr

Source§

fn from(e: ExprAssignOp) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprBinary> for Expr

Source§

fn from(e: ExprBinary) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprBlock> for Expr

Source§

fn from(e: ExprBlock) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprBox> for Expr

Source§

fn from(e: ExprBox) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprBreak> for Expr

Source§

fn from(e: ExprBreak) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprCall> for Expr

Source§

fn from(e: ExprCall) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprCast> for Expr

Source§

fn from(e: ExprCast) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprCatch> for Expr

Source§

fn from(e: ExprCatch) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprClosure> for Expr

Source§

fn from(e: ExprClosure) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprContinue> for Expr

Source§

fn from(e: ExprContinue) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprField> for Expr

Source§

fn from(e: ExprField) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprForLoop> for Expr

Source§

fn from(e: ExprForLoop) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprGroup> for Expr

Source§

fn from(e: ExprGroup) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprIf> for Expr

Source§

fn from(e: ExprIf) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprIfLet> for Expr

Source§

fn from(e: ExprIfLet) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprInPlace> for Expr

Source§

fn from(e: ExprInPlace) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprIndex> for Expr

Source§

fn from(e: ExprIndex) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprLit> for Expr

Source§

fn from(e: ExprLit) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprLoop> for Expr

Source§

fn from(e: ExprLoop) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprMacro> for Expr

Source§

fn from(e: ExprMacro) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprMatch> for Expr

Source§

fn from(e: ExprMatch) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprMethodCall> for Expr

Source§

fn from(e: ExprMethodCall) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprParen> for Expr

Source§

fn from(e: ExprParen) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprPath> for Expr

Source§

fn from(e: ExprPath) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprRange> for Expr

Source§

fn from(e: ExprRange) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprRepeat> for Expr

Source§

fn from(e: ExprRepeat) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprReturn> for Expr

Source§

fn from(e: ExprReturn) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprStruct> for Expr

Source§

fn from(e: ExprStruct) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprTry> for Expr

Source§

fn from(e: ExprTry) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprTuple> for Expr

Source§

fn from(e: ExprTuple) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprType> for Expr

Source§

fn from(e: ExprType) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprUnary> for Expr

Source§

fn from(e: ExprUnary) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprUnsafe> for Expr

Source§

fn from(e: ExprUnsafe) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprVerbatim> for Expr

Source§

fn from(e: ExprVerbatim) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprWhile> for Expr

Source§

fn from(e: ExprWhile) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprWhileLet> for Expr

Source§

fn from(e: ExprWhileLet) -> Expr

Converts to this type from the input type.
Source§

impl From<ExprYield> for Expr

Source§

fn from(e: ExprYield) -> Expr

Converts to this type from the input type.
Source§

impl Hash for Expr

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Expr

Source§

fn eq(&self, other: &Expr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Synom for Expr

Source§

fn parse(i: Cursor<'_>) -> PResult<'_, Self>

Source§

fn description() -> Option<&'static str>

Source§

impl ToTokens for Expr

Source§

fn to_tokens(&self, tokens: &mut Tokens)

Write self to the given Tokens. Read more
Source§

fn into_tokens(self) -> Tokens
where Self: Sized,

Convert self directly into a Tokens object. Read more
Source§

impl Eq for Expr

Source§

impl StructuralPartialEq for Expr

Auto Trait Implementations§

§

impl Freeze for Expr

§

impl RefUnwindSafe for Expr

§

impl !Send for Expr

§

impl !Sync for Expr

§

impl Unpin for Expr

§

impl UnwindSafe for Expr

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Spanned for T
where T: ToTokens,

Source§

fn span(&self) -> Span

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.