Struct File

pub struct File { pub shebang: Option<String>, pub frontmatter: Option<Frontmatter>, pub attrs: Vec<Attribute>, pub items: Vec<Item> }

A complete file of Rust source code.

Typically File objects are created with parse_file.

Example

Parse a Rust source file into a syn::File and print out a debug representation of the syntax tree.

use std::env;
use std::fs;
use std::process;

fn main() {
# }
#
# fn fake_main() {
    let mut args = env::args();
    let _ = args.next(); // executable name

    let filename = match (args.next(), args.next()) {
        (Some(filename), None) => filename,
        _ => {
            eprintln!("Usage: dump-syntax path/to/filename.rs");
            process::exit(1);
        }
    };

    let src = fs::read_to_string(&filename).expect("unable to read file");
    let syntax = syn::parse_file(&src).expect("unable to parse file");

    // Debug impl is available if Syn is built with "extra-traits" feature.
    println!("{:#?}", syntax);
}

Running with its own source code as input, this program prints output that begins with:

File {
    shebang: None,
    frontmatter: None,
    attrs: [],
    items: [
        Item::Use {
            attrs: [],
            vis: Visibility::Inherited,
            use_token: Token![use],
            leading_colon: None,
            tree: UseTree::Path(
                UsePath {
                    ident: Ident(
                        std,
                    ),
                    colon2_token: Token![::],
                    tree: UseTree::Name(
                        UseName {
                            ident: Ident(
                                env,
                            ),
                        },
                    ),
                },
            ),
            semi_token: Token![;],
        },
...

Fields

shebang: Option<String>
frontmatter: Option<Frontmatter>
attrs: Vec<Attribute>
items: Vec<Item>

Trait Implementations

impl Clone for File

fn clone(&self) -> Self

impl Parse for File

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

impl ToTokens for File

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

Auto Trait Implementations

impl !Send for File

impl !Sync for File

impl Freeze for File

impl RefUnwindSafe for File

impl Unpin for File

impl UnsafeUnpin for File

impl UnwindSafe for File

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for File

fn from(t: T) -> T

Returns the argument unchanged.

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

fn span(&self) -> Span

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

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

impl<T, U> Into<U> for File 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 File where U: Into<T>,

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

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

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