Enum syn::Expr

source ·
#[non_exhaustive]
pub enum Expr {
Show 39 variants Array(ExprArray), Assign(ExprAssign), Async(ExprAsync), Await(ExprAwait), Binary(ExprBinary), Block(ExprBlock), Break(ExprBreak), Call(ExprCall), Cast(ExprCast), Closure(ExprClosure), Const(ExprConst), Continue(ExprContinue), Field(ExprField), ForLoop(ExprForLoop), Group(ExprGroup), If(ExprIf), Index(ExprIndex), Infer(ExprInfer), 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), 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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Array(ExprArray)

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

§

Assign(ExprAssign)

An assignment expression: a = compute().

§

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

§

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.

§

Const(ExprConst)

A const block: const { ... }.

§

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

§

Infer(ExprInfer)

The inferred value of a const generic argument, denoted _.

§

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

§

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§

source§

impl Clone for Expr

source§

fn clone(&self) -> Self

Returns a copy 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, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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<ExprAsync> for Expr

source§

fn from(e: ExprAsync) -> Expr

Converts to this type from the input type.
source§

impl From<ExprAwait> for Expr

source§

fn from(e: ExprAwait) -> 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<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<ExprClosure> for Expr

source§

fn from(e: ExprClosure) -> Expr

Converts to this type from the input type.
source§

impl From<ExprConst> for Expr

source§

fn from(e: ExprConst) -> 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<ExprIndex> for Expr

source§

fn from(e: ExprIndex) -> Expr

Converts to this type from the input type.
source§

impl From<ExprInfer> for Expr

source§

fn from(e: ExprInfer) -> Expr

Converts to this type from the input type.
source§

impl From<ExprLet> for Expr

source§

fn from(e: ExprLet) -> 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<ExprReference> for Expr

source§

fn from(e: ExprReference) -> 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<ExprTryBlock> for Expr

source§

fn from(e: ExprTryBlock) -> 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<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<ExprWhile> for Expr

source§

fn from(e: ExprWhile) -> 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>(&self, state: &mut H)where H: Hasher,

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 Parse for Expr

source§

fn parse(input: ParseStream<'_>) -> Result<Self>

source§

impl PartialEq<Expr> for Expr

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl ToTokens for Expr

source§

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

Write self to the given TokenStream. Read more
source§

fn to_token_stream(&self) -> TokenStream

Convert self directly into a TokenStream object. Read more
source§

fn into_token_stream(self) -> TokenStreamwhere Self: Sized,

Convert self directly into a TokenStream object. Read more
source§

impl Eq for Expr

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§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Spanned + ?Sized,

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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.