= std::fs::read("Cargo.toml").unwrap(); let content = String::from_utf8(content).unwrap(); let value = content.parse::<toml::Value>().unwrap(); dbg!(value); }
{ let content = std::fs::read("Cargo.toml")?; let content = String::from_utf8(content)?; // `?` couldn't convert the error to `std::io::Error` let value = content.parse::<toml::Value>()?; dbg!(value); Ok(()) }
{ let content = std::fs::read("Cargo.toml")?; let content = String::from_utf8(content)?; let value = content.parse::<toml::Value>()?; dbg!(value); Ok(()) }
directory って何が? fn main() -> Result<(), anyhow::Error> { let content = std::fs::read("a.toml")?; // 存在しないファイルを読んでみる let content = String::from_utf8(content)?; let value = content.parse::<toml::Value>()?; dbg!(value); Ok(()) } Error: No such file or directory (os error 2)
{ let content = std::fs::read("a.toml").context("failed to read a.toml")?; let content = String::from_utf8(content)?; let value = content.parse::<toml::Value>()?; dbg!(value); Ok(()) } Error: failed to read a.toml Caused by: No such file or directory (os error 2)
Result<(), anyhow::Error> { let content = std::fs::read("a.toml").context("failed to read a.toml")?; let content = String::from_utf8(content).context("failed to convert to UTF-8")?; let value = content .parse::<toml::Value>() .with_context(|| format!("failed to parse as TOML:\n{}", content))?; dbg!(value); Ok(()) }
-> anyhow::Result<()> { let args = std::env::args().len(); ensure!(args > 2, "args must be more than 2"); if args > 10 { bail!("too many args"); } Err(anyhow!("quit")) }