简体   繁体   English

如何匹配Rust中的&'static str

[英]How to match against a &'static str in Rust

I am a Rust beginner and I can't solve this type problem. 我是Rust初学者,我无法解决这类问题。 I have tried replacing &name with name , but the error "pattern &_ not covered" occurred. 我曾尝试更换&namename ,但这个错误“的格局&_不覆盖”的发生。

fn get_project(name: &'static str) {
    match &name {
        "hi" => {},
    }
}

fn main() {
    let project = get_project("hi");
}

Compiler error: 编译错误:

error[E0308]: mismatched types
 --> <anon>:3:9
  |
3 |         "hi" => {},
  |         ^^^^ expected &str, found str
  |
  = note: expected type `&&str`
  = note:    found type `&'static str`

String literals – like "hi" – have the type &'static str . 字符串文字 - 比如"hi" - 具有类型&'static str So if you already have a &str , you don't need to add the & : 所以如果你已经有一个&str ,你不需要添加&

fn get_project(name: &str) {
    match name {
        "hi" => {},
        _ => {}, // matches have to be exhaustive 
    }
}

I also added a default case , because matches in Rust need to be exhaustive: they need to cover all possible cases. 我还添加了一个默认情况 ,因为Rust中的匹配需要详尽无遗:它们需要涵盖所有可能的情况。


Maybe you noticed, that I also removed the 'static from the argument list. 也许你注意到,我也从参数列表中删除了'static If you want to read about some lifetime stuff, go ahead. 如果你想了解一些有生命的东西,请继续。 Else, stop reading here, because it's possibly confusing and not that important in this case. 否则,请在这里停止阅读,因为它可能令人困惑,在这种情况下并不重要。

In this function there is no need to restrict the lifetime of the given argument to 'static . 在此函数中,不需要将给定参数的生命周期限制为'static Maybe you also want to pass in string slices that are borrowed from a String : 也许你还想传入从String借来的字符串切片:

let user_input = read_user_input();  // type `String`
get_project(&input);

The code above only works when you remove the 'static from the argument. 上面的代码仅在从参数中删除'static时才有效。 Once removed, the function is equivalent to: 删除后,该功能相当于:

fn get_project<'a>(name: &'a str) { ... }

This means that the function is generic over a lifetime 'a . 这意味着该函数在一生中是通用的'a The function says: given any lifetime 'a , you can give me a string with said lifetime and I am able to do my thing. 函数说:给定任何一个生命周期'a ,你可以给我一个字符串表示生命周期,我能够做我的事情。 Which is true. 这是真的。 If the function wouldn't be able to do it for any lifetime, the compiler would complain ;-) 如果函数无法在任何生命周期内执行,编译器会抱怨;-)

In your example, name doesn't need to have a static lifetime. 在您的示例中, name不需要具有static生存期。 Because you only use name inside your function, name doesn't need to have an extended lifetime. 因为您只在函数内使用name ,所以name不需要具有延长的生命周期。 Check out the strings chapter of The Rust Programming Language . 查看Rust编程语言字符串章节 To match a &str with a &'static str you don't need & , just the variable itself is enough. 要将&str&'static str匹配,你不需要& ,只需变量本身就足够了。

pub fn get_project(name: &str) {
    match name {
        "hi" => println!("I found hi!"),
        _ => println!("Nothing match"),
    }
}

fn main() {
    get_project("hi");
    get_project("42");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM