Expected String, found &str when matching an optional string

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"),
        }                                                                          
    }                                                                           
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)