Main function
[main.incl: (P1)](https://github.com/john-cd/rust_howto/issues/549)
use std::fs::File; use std::io::Read; use anyhow::Result; use anyhow::anyhow; fn read_uptime() -> Result<u64> { let mut uptime = String::new(); File::open("/proc/uptime")?.read_to_string(&mut uptime)?; Ok(uptime .split('.') .next() .ok_or(anyhow!("Cannot parse uptime data"))? .parse()?) } fn main() { match read_uptime() { Ok(uptime) => println!("uptime: {} seconds", uptime), Err(err) => eprintln!("error: {}", err), }; }
Async main function
use anyhow::Result; #[tokio::main] async fn main() -> Result<()> { println!("I'm async!"); Ok(()) }
[main: add text (P1)](https://github.com/john-cd/rust_howto/issues/550)