Function parse_file

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

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.

Examples

use std::error::Error;
use std::fs;
use std::io::Read;

fn run() -> Result<(), Box<dyn Error>> {
    let content = fs::read_to_string("path/to/code.rs")?;
    let ast = syn::parse_file(&content)?;
    if let Some(shebang) = ast.shebang {
        println!("{}", shebang);
    }
    println!("{} items", ast.items.len());

    Ok(())
}
#
# run().unwrap();