To find the characters in the first string that are not present in the second string in Rust, you can use sets and iterators.
In this example,
str1
and str2
with different values.HashSet
type from the std::collections
module.difference
method on the sets to find characters not present in the second set.// Find Characters Not Present in Second String
use std::collections::HashSet;
fn main() {
let str1 = String::from("Hello World");
let str2 = String::from("apple");
let set1: HashSet<char> = str1.chars().collect();
let set2: HashSet<char> = str2.chars().collect();
let diff: HashSet<_> = set1.difference(&set2).copied().collect();
let result: String = diff.into_iter().collect();
println!("Characters not present in second string: {}", result);
}
Characters not present in second string: Ho Word
In this tutorial, we learned How to find the Characters in the First String that are not Present in the Second String in Rust language with well detailed examples.