string 如何在 Rust 中将字符串与字符串文字进行匹配?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25383488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:16:04  来源:igfitidea点击:

How to match a String against string literals in Rust?

stringmatchrust

提问by Jeroen

I'm trying to figure out how to match a Stringin Rust.

我想弄清楚如何String在 Rust 中匹配 a 。

I initially tried matching like this, but I figured out Rust cannot implicitly cast from std::string::Stringto &str.

我最初尝试这样匹配,但我发现 Rust 不能隐式地从std::string::Stringto 转换&str

fn main() {
    let stringthing = String::from("c");
    match stringthing {
        "a" => println!("0"),
        "b" => println!("1"),
        "c" => println!("2"),
    }
}

This has the error:

这有错误:

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
4 |         "a" => println!("0"),
  |         ^^^ expected struct `std::string::String`, found reference
  |
  = note: expected type `std::string::String`
             found type `&'static str`

I then tried to construct new Stringobjects, as I could not find a function to cast a Stringto a &str.

然后我尝试构造新String对象,因为我找不到将 aString转换为a 的函数&str

fn main() {
    let stringthing = String::from("c");
    match stringthing {
        String::from("a") => println!("0"),
        String::from("b") => println!("1"),
        String::from("c") => println!("2"),
    }
}

This gave me the following error 3 times:

这给了我以下错误 3 次:

error[E0164]: `String::from` does not name a tuple variant or a tuple struct
 --> src/main.rs:4:9
  |
4 |         String::from("a") => return 0,
  |         ^^^^^^^^^^^^^^^^^ not a tuple variant or struct

How to actually match Strings in Rust?

如何String在 Rust 中实际匹配s?

采纳答案by Anonymous Coward

You can do something like this:

你可以这样做:

match &stringthing[..] {
    "a" => println!("0"),
    "b" => println!("1"),
    "c" => println!("2"),
    _ => println!("something else!"),
}

There's also an as_strmethod as of Rust 1.7.0:

as_str从 Rust 1.7.0 开始,还有一个方法:

match stringthing.as_str() {
    "a" => println!("0"),
    "b" => println!("1"),
    "c" => println!("2"),
    _ => println!("something else!"),
}

回答by Tijs Maas

as_sliceis deprecated, you should now use the trait std::convert::AsRefinstead:

as_slice已弃用,您现在应该改用 trait std::convert::AsRef

match stringthing.as_ref() {
    "a" => println!("0"),
    "b" => println!("1"),
    "c" => println!("2"),
    _ => println!("something else!"),
}

Note that you also have to explicitly handle the catch-all case.

请注意,您还必须明确处理包罗万象的情况。

回答by Marco Scannadinari

You could also do

你也可以这样做

match &stringthing as &str {
    "a" => println!("0"),
    "b" => println!("1"),
    "c" => println!("2"),
    _ => println!("something else!"),
}

See:

看:

回答by A.B.

Editor's note: This answer pertains to an version of Rust before 1.0 and does not work in Rust 1.0

编者注:此答案适用于 1.0 之前的 Rust 版本,不适用于 Rust 1.0

You can match on a string slice.

您可以匹配字符串切片。

match stringthing.as_slice() {
    "a" => println!("0"),
    "b" => println!("1"),
    "c" => println!("2"),
    _ => println!("something else!"),
}

回答by omrihhh

You can try:

你可以试试:

fn main() {
    let stringthing = String::from("c");
    match &*stringthing {
        "a" => println!("0"),
        "b" => println!("1"),
        "c" => println!("2"),
        _ => println!("else")
    }
}