How to match a String against string literals?
@lebyy▲ 0 net votes
Problem: I'm trying to figure out how to match a String in Rust. I initially tried matching like this, but I figured out Rust cannot implicitly cast from std::string::String to &str . fn main() { let stringthing = String::from("c"); match stringthing { "a" =>… Fix (from the top answer): UPDATE: Use .as_str() like this to convert the String to an &str : match stringthing.as_str() { "a" => println!("0"), "b" => println!("1"), & Reason .as_str() is more concise and enforces stricter type checking. The trait as_ref is implemented for multiple types and its behaviour could be changed for type String ,…