Struct File

struct File { ... }

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,
    attrs: [],
    items: [
        Use(
            ItemUse {
                attrs: [],
                vis: Inherited,
                use_token: Use,
                leading_colon: None,
                tree: Path(
                    UsePath {
                        ident: Ident(
                            std,
                        ),
                        colon2_token: Colon2,
                        tree: Name(
                            UseName {
                                ident: Ident(
                                    env,
                                ),
                            },
                        ),
                    },
                ),
                semi_token: Semi,
            },
        ),
...

Fields

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

Implementations

impl Clone for File

fn clone(self: &Self) -> Self

impl Debug for File

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

impl Eq for File

impl Freeze for File

impl Hash for File

fn hash<H>(self: &Self, state: &mut H)
where
    H: Hasher

impl Parse for File

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

impl PartialEq for File

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

impl RefUnwindSafe for File

impl Send for File

impl Sync for File

impl ToTokens for File

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

impl Unpin for File

impl UnsafeUnpin for File

impl UnwindSafe for File

impl<T> Any for File

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for File

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for File

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

impl<T> CloneToUninit for File

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

impl<T> From for File

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Spanned for File

fn span(self: &Self) -> Span

impl<T> ToOwned for File

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for File

fn into(self: 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 for File

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

impl<T, U> TryInto for File

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