Rust futures:修订间差异

来自WHY42
Riguz留言 | 贡献
创建页面,内容为“ Category:Rust
 
Riguz留言 | 贡献
无编辑摘要
标签2017版源代码编辑
第1行: 第1行:
= Result =
== Unwrap ==
<syntaxhighlight lang="rust">
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
}
</syntaxhighlight>




[[Category:Rust]]
[[Category:Rust]]

2024年12月28日 (六) 11:29的版本

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
}