Expected String, found &str when matching an optional string

情到浓时终转凉″ 提交于 2019-12-19 05:33:51

问题


I am trying to write a simple function in Rust that will ask user a question expecting answer of "you" or "me". It should return a boolean value or ask again if the user answers wrong. I came up with:

fn player_starts() -> bool {                                                    
    println!("Who will start (me/you)");                                       
    loop {                                                                      
        let input = readline::readline(">");                                    
        match input {                                                           
            Some("me") => return true,                                          
            Some("you") => return false,                                        
            _ => None,                                                          
        }                                                                          
    }                                                                           
}       

What I get is:

error: mismatched types:
 expected `collections::string::String`,
    found `&'static str`
(expected struct `collections::string::String`,
found &-ptr) [E0308]

Is there some way to coerce the literal to work here or is there some better way to achieve my goal?


回答1:


This should work:

fn player_starts() -> bool {                      
    println!("Who will start me/you)");                    
    loop {
        let input = readline::readline(">");
        match input.as_ref().map(String::as_ref) {
            Some("me") => return true,
            Some("you") => return false,
            _ => ()
        }
    }
}

Note the expression in the match statement, where we convert from an Option<String> to an Option<&str>.




回答2:


The way you usually convert a &str to a String is to_owned, e.g.

"me".to_owned()

However, you can't do pattern matching on a String. You could expect a success, get a &str from the String then pattern match on that:

fn player_starts() -> bool {                                                    
    println!("Who will start (me/you)");                                       
    loop {                                                                      
        let input = readline::readline(">");
        match input.expect("Failed to read line").as_ref() {
            "me" => return true,                                          
            "you" => return false,
            _ => println!("Enter me or you"),
        }                                                                          
    }                                                                           
}


来源:https://stackoverflow.com/questions/32500974/expected-string-found-str-when-matching-an-optional-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!