pub fn parse_file(content: &str) -> Result<File>
Available on crate features
parsing
and full
only.Expand description
Parse the content of a file of Rust code.
This is different from syn::parse_str::<File>(content)
in two ways:
- It discards a leading byte order mark
\u{FEFF}
if the file has one. - It preserves the shebang line of the file, such as
#!/usr/bin/env rustx
.
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(())
}