Struct Block

pub struct Block { pub brace_token: Brace, pub stmts: Vec<Stmt> }

A braced block containing Rust statements.

Fields

brace_token: Brace
stmts: Vec<Stmt>

Statements in a block

Implementations

impl Block

fn parse_within(input: ParseStream<'_>) -> Result<Vec<Stmt>>

Parse the body of a block as zero or more statements, possibly including one trailing expression.

Example

use syn::{braced, token, Attribute, Block, Ident, Result, Stmt, Token};
use syn::parse::{Parse, ParseStream};

// Parse a function with no generics or parameter list.
//
//     fn playground {
//         let mut x = 1;
//         x += 1;
//         println!("{}", x);
//     }
struct MiniFunction {
    attrs: Vec<Attribute>,
    fn_token: Token![fn],
    name: Ident,
    brace_token: token::Brace,
    stmts: Vec<Stmt>,
}

impl Parse for MiniFunction {
    fn parse(input: ParseStream) -> Result<Self> {
        let outer_attrs = input.call(Attribute::parse_outer)?;
        let fn_token: Token![fn] = input.parse()?;
        let name: Ident = input.parse()?;

        let content;
        let brace_token = braced!(content in input);
        let inner_attrs = content.call(Attribute::parse_inner)?;
        let stmts = content.call(Block::parse_within)?;

        Ok(MiniFunction {
            attrs: {
                let mut attrs = outer_attrs;
                attrs.extend(inner_attrs);
                attrs
            },
            fn_token,
            name,
            brace_token,
            stmts,
        })
    }
}

Trait Implementations

impl Clone for Block

fn clone(&self) -> Self

impl Parse for Block

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

impl ToTokens for Block

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

Auto Trait Implementations

impl !Send for Block

impl !Sync for Block

impl Freeze for Block

impl RefUnwindSafe for Block

impl Unpin for Block

impl UnsafeUnpin for Block

impl UnwindSafe for Block

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for Block

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Spanned for Block where T: Spanned + ?Sized,

fn span(&self) -> Span

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn into(self) -> U

Calls U::from(self).

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

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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