To check if a string ends with a specific suffix in Rust, you can use the ends_with()
method of the str
type.
In this example,
str1
with a value of "Hello World"
.ends_with()
method of the str
type with the suffix "World"
.ends_with()
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.fn main() {
let str1 = "Hello World";
let ends_with_world = str1.ends_with("World");
println!("String ends with suffix 'World': {:?}", ends_with_world);
}
String ends with suffix 'World': true
In this example,
str2
with a value of "apple"
.ends_with()
method of the str
type with the suffix "banana"
.ends_with()
method returns true
if the string ends with the specified suffix; otherwise, it returns false
.fn main() {
let str2 = "apple";
let ends_with_banana = str2.ends_with("banana");
println!("String ends with suffix 'banana': {:?}", ends_with_banana);
}
String ends with suffix 'banana': false
In this tutorial, we learned How to Check if String ends with Specific Suffix in Rust language with well detailed examples.