[][src]Function syn::parse_file

pub fn parse_file(content: &str) -> Result<File, ParseError>

Parse the content of a file of Rust code.

This is different from syn::parse_str::<File>(content) in two ways:

If present, either of these would be an error using from_str.

This function is available if Syn is built with the "parsing" and "full" features.

Examples

extern crate syn;

use std::fs::File;
use std::io::Read;

fn run() -> Result<()> {
    let mut file = File::open("path/to/code.rs")?;
    let mut content = String::new();
    file.read_to_string(&mut content)?;

    let ast = syn::parse_file(&content)?;
    if let Some(shebang) = ast.shebang {
        println!("{}", shebang);
    }
    println!("{} items", ast.items.len());

    Ok(())
}