rust 如何将字符串与字符串常量进行匹配?

np8igboo  于 2022-12-23  发布在  其他
关注(0)|答案(8)|浏览(182)

我正在想办法匹配Rust中的String
我最初尝试过这样的匹配,但发现Rust不能隐式地从std::string::String转换为&str

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

这有错误:

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`

然后,我尝试构造新的String对象,因为我找不到将String转换为&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"),
    }
}

这给了我以下错误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

如何在Rust中真正匹配String

btxsgosb

btxsgosb1#

**更新:**按如下方式使用.as_str()String转换为&str

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

原因.as_str()更简洁,并且强制执行更严格的类型检查。trait as_ref是为多个类型实现的,其行为可能会为类型String更改,从而导致意外结果。同样,如果输入参数更改类型,则编译器在该类型实现trait as_ref时不会发出问题信号。

文档建议使用as_str以及https://doc.rust-lang.org/std/string/struct.String.htmlhttps://doc.rust-lang.org/std/primitive.str.html

旧答案:

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

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

注意,您还必须显式地处理捕获所有情况。

oalqel3c

oalqel3c2#

你可以这样做:

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

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

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

izkcnapc3#

你也可以

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

参见:

4ngedf3f

4ngedf3f4#

编者按:此答案适用于Rust 1.0之前的版本,在Rust 1.0中不适用
可以在字符串切片上进行匹配。

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

a2mppw5e5#

您可以尝试:

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

qfe3c7zg6#

您可以通过以下操作将String转换为&str

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

ujv3wf0j7#

在字符串上使用as_str()获取字符串切片

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

如果您从控制台获取输入并希望对其执行匹配,请确保在as_str()之后调用trim()以从输入中删除转义字符,即'\n'。

match stringthing.as_str().trim() {...}
5cg8jx4n

5cg8jx4n8#

可以使用as_str()方法将String转换为&str,然后匹配&str值,如下所示:

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

或者,您可以将String值绑定到变量,然后在变量上进行匹配,如下所示:

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

或者,您可以使用==运算符将String值与字符串文字量进行比较,如下所示:

fn main() {
    let stringthing = String::from("c");
    if stringthing == "a" {
        println!("0");
    } else if stringthing == "b" {
        println!("1");
    } else if stringthing == "c" {
        println!("2");
    } else {
        println!("other");
    }
}

相关问题