We can use the contains()
method of Rust to check if a string contains a particular match or sub-string. It takes only one parameter, which is the sub-string or matches it wants to check.
string.contains(substring)
substring
: This is the string we want to check to see if it occurs in another string.
The value returned is a Boolean value. A true
is returned if substring
is present, or is contained inside another string. Otherwise, false
is returned.
fn main() {// create some stringslet str1 = "Rust";let str2 = "Educative is the best platform!";let str3 = "Welcome to Edpresso";let str4 = "I am 400 years old";// check if some substrings are contained in the ones createdprintln!("{}", str1.contains("ust"));println!("{}", str2.contains("Educative"));println!("{}", str3.contains("the"));println!("{}", str4.contains("400"));}
In the code above: