# [−] [src] Enum syn::Expr

    pub enum Expr {
        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
    }

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

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

## Implementations

### `impl Expr` [src]

#### `pub fn parse_without_eager_brace(input: ParseStream<'_>) -> Result<Expr>` [src]

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

### `impl Clone for Expr` [src]

#### `fn clone(&self) -> Self` [src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`1.0.0 [src]

Performs copy-assignment from `source`. Read more

### `impl From<ExprArray> for Expr` [src]

#### `fn from(e: ExprArray) -> Expr` [src]

Performs the conversion.

### `impl From<ExprAssign> for Expr` [src]

#### `fn from(e: ExprAssign) -> Expr` [src]

Performs the conversion.

### `impl From<ExprAssignOp> for Expr` [src]

#### `fn from(e: ExprAssignOp) -> Expr` [src]

Performs the conversion.

### `impl From<ExprAsync> for Expr` [src]

#### `fn from(e: ExprAsync) -> Expr` [src]

Performs the conversion.

### `impl From<ExprAwait> for Expr` [src]

#### `fn from(e: ExprAwait) -> Expr` [src]

Performs the conversion.

### `impl From<ExprBinary> for Expr` [src]

#### `fn from(e: ExprBinary) -> Expr` [src]

Performs the conversion.

### `impl From<ExprBlock> for Expr` [src]

#### `fn from(e: ExprBlock) -> Expr` [src]

Performs the conversion.

### `impl From<ExprBox> for Expr` [src]

#### `fn from(e: ExprBox) -> Expr` [src]

Performs the conversion.

### `impl From<ExprBreak> for Expr` [src]

#### `fn from(e: ExprBreak) -> Expr` [src]

Performs the conversion.

### `impl From<ExprCall> for Expr` [src]

#### `fn from(e: ExprCall) -> Expr` [src]

Performs the conversion.

### `impl From<ExprCast> for Expr` [src]

#### `fn from(e: ExprCast) -> Expr` [src]

Performs the conversion.

### `impl From<ExprClosure> for Expr` [src]

#### `fn from(e: ExprClosure) -> Expr` [src]

Performs the conversion.

### `impl From<ExprContinue> for Expr` [src]

#### `fn from(e: ExprContinue) -> Expr` [src]

Performs the conversion.

### `impl From<ExprField> for Expr` [src]

#### `fn from(e: ExprField) -> Expr` [src]

Performs the conversion.

### `impl From<ExprForLoop> for Expr` [src]

#### `fn from(e: ExprForLoop) -> Expr` [src]

Performs the conversion.

### `impl From<ExprGroup> for Expr` [src]

#### `fn from(e: ExprGroup) -> Expr` [src]

Performs the conversion.

### `impl From<ExprIf> for Expr` [src]

#### `fn from(e: ExprIf) -> Expr` [src]

Performs the conversion.

### `impl From<ExprIndex> for Expr` [src]

#### `fn from(e: ExprIndex) -> Expr` [src]

Performs the conversion.

### `impl From<ExprLet> for Expr` [src]

#### `fn from(e: ExprLet) -> Expr` [src]

Performs the conversion.

### `impl From<ExprLit> for Expr` [src]

#### `fn from(e: ExprLit) -> Expr` [src]

Performs the conversion.

### `impl From<ExprLoop> for Expr` [src]

#### `fn from(e: ExprLoop) -> Expr` [src]

Performs the conversion.

### `impl From<ExprMacro> for Expr` [src]

#### `fn from(e: ExprMacro) -> Expr` [src]

Performs the conversion.

### `impl From<ExprMatch> for Expr` [src]

#### `fn from(e: ExprMatch) -> Expr` [src]

Performs the conversion.

### `impl From<ExprMethodCall> for Expr` [src]

#### `fn from(e: ExprMethodCall) -> Expr` [src]

Performs the conversion.

### `impl From<ExprParen> for Expr` [src]

#### `fn from(e: ExprParen) -> Expr` [src]

Performs the conversion.

### `impl From<ExprPath> for Expr` [src]

#### `fn from(e: ExprPath) -> Expr` [src]

Performs the conversion.

### `impl From<ExprRange> for Expr` [src]

#### `fn from(e: ExprRange) -> Expr` [src]

Performs the conversion.

### `impl From<ExprReference> for Expr` [src]

#### `fn from(e: ExprReference) -> Expr` [src]

Performs the conversion.

### `impl From<ExprRepeat> for Expr` [src]

#### `fn from(e: ExprRepeat) -> Expr` [src]

Performs the conversion.

### `impl From<ExprReturn> for Expr` [src]

#### `fn from(e: ExprReturn) -> Expr` [src]

Performs the conversion.

### `impl From<ExprStruct> for Expr` [src]

#### `fn from(e: ExprStruct) -> Expr` [src]

Performs the conversion.

### `impl From<ExprTry> for Expr` [src]

#### `fn from(e: ExprTry) -> Expr` [src]

Performs the conversion.

### `impl From<ExprTryBlock> for Expr` [src]

#### `fn from(e: ExprTryBlock) -> Expr` [src]

Performs the conversion.

### `impl From<ExprTuple> for Expr` [src]

#### `fn from(e: ExprTuple) -> Expr` [src]

Performs the conversion.

### `impl From<ExprType> for Expr` [src]

#### `fn from(e: ExprType) -> Expr` [src]

Performs the conversion.

### `impl From<ExprUnary> for Expr` [src]

#### `fn from(e: ExprUnary) -> Expr` [src]

Performs the conversion.

### `impl From<ExprUnsafe> for Expr` [src]

#### `fn from(e: ExprUnsafe) -> Expr` [src]

Performs the conversion.

### `impl From<ExprWhile> for Expr` [src]

#### `fn from(e: ExprWhile) -> Expr` [src]

Performs the conversion.

### `impl From<ExprYield> for Expr` [src]

#### `fn from(e: ExprYield) -> Expr` [src]

Performs the conversion.

### `impl Parse for Expr` [src]

#### `fn parse(input: ParseStream<'_>) -> Result<Self>` [src]

### `impl ToTokens for Expr` [src]

#### `fn to_tokens(&self, tokens: &mut TokenStream)` [src]

Write `self` to the given `TokenStream`. Read more

#### `fn to_token_stream(&self) -> TokenStream` [src]

Convert `self` directly into a `TokenStream` object. Read more

#### `fn into_token_stream(self) -> TokenStream` [src]

Convert `self` directly into a `TokenStream` object. Read more

## 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

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

### `impl<T> From<T> for T` [src]

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `pub fn into(self) -> U` [src]

Performs the conversion.

### `impl<T> Spanned for T where
T: Spanned + ?Sized,` [src]

#### `pub fn span(&Self) -> Span` [src]

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

### `impl<T> ToOwned for T where
T: Clone,` [src]

#### `type Owned = T`

The resulting type after obtaining ownership.

#### `pub fn to_owned(&self) -> T` [src]

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

#### `pub fn clone_into(&self, target: &mut T)` [src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into`)

recently added

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

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

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

The type returned in the event of a conversion error.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.