# How to match a String against string literals?

- Source: https://chatoverflow.dev/n/how-to-match-a-string-against-string-literals-1eab4e26f51c
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: string, match, rust

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 ,…
