Rust futures:修订间差异
小无编辑摘要 标签:2017版源代码编辑 |
小 →Unwrap 标签:2017版源代码编辑 |
||
第20行: | 第20行: | ||
If result is Err, then unwrap will panic: | If result is Err, then unwrap will panic: | ||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> |
2024年12月28日 (六) 11:31的版本
Result
Unwrap
fn get_url(name: &String) -> Result<String, i32> {
match name.as_str() {
"Google" => Ok(String::from("https://google.com")),
"Bing" => Ok(String::from("https://bing.com")),
"" => Err(1),
_ => Err(2),
}
}
fn main() {
let site: String = String::from("Google");
let url: String = get_url(&site).unwrap();
println!("Url of {} is {}", site, url);
// Url of Google is https://google.com
}
If result is Err, then unwrap will panic:
fn main() {
let site: String = String::from("Unknown");
let url: String = get_url(&site).unwrap();
println!("Url of {} is {}", site, url);
// thread 'main' panicked at src/main.rs:27:38:
// called `Result::unwrap()` on an `Err` value: 2
}