How to check if a character exists in a string with Rust

1 Answer

0 votes
fn main() {
    // Define the string we want to search in
    let s = "rustlang";

    // Check for a character using contains()
    let has_r = s.contains('r'); // true
    let has_z = s.contains('z'); // false

    // Print the raw boolean results
    println!("{}", has_r);
    println!("{}", has_z);

    // Conditional check
    if s.contains('r') {
        println!("exists");
    } else {
        println!("not exists");
    }
}



/*
run:

true
false
exists

*/

 



answered 3 hours ago by avibootz
...