xxxxxxxxxx
// You can use .unwrap_or_else method instead of Result enum
use std::path::Path;
use std::fs;
fn main() {
let path = Path::new("hello.txt");
let content = String::from("hello world");
fs::write(path, content).unwrap_or_else(|error| {
panic!("an error occurred while writing the file: {}", error)
})
}
xxxxxxxxxx
// You can use .unwrap_or_else method instead of Result enum
use std::path::Path;
use std::fs;
fn main() {
let path = Path::new("hello.txt");
let content = String::from("hello world");
fs::write(path, content).unwrap_or_else(|error| {
panic!("an error occurred while writing the file: {}", error)
})
}
xxxxxxxxxx
// You can use .unwrap_or_else method instead of Result enum
use std::path::Path;
use std::fs;
fn main() {
let path = Path::new("hello.txt");
let content = String::from("hello world");
fs::write(path, content).unwrap_or_else(|error| {
panic!("an error occurred while writing the file: {}", error)
})
}
xxxxxxxxxx
// unwrap_or_else() might be better becuase of eagerness
fn main() {
let _one = None.unwrap_or("one".to_string());
let _two = None.unwrap_or_else(|| "two".to_string());
}