Match the end of a string
Check the letters at the end of the text. The result is true only when those letters match.
str::ends_with
Simple example
Does abc end with c?
Start'abc' · 'c'
Resulttrue
fn main() {
println!("{}", "abc".ends_with("c"));
}
true
Advertisements
Everyday examples
A teacher wants PDF files. Does this name end in .pdf?
Start'notes.pdf' · '.pdf'
Resulttrue
fn main() {
println!("{}", "notes.pdf".ends_with(".pdf"));
}
true
A music player lists MP3 files. Does the recording have that ending?
Start'rehearsal.wav' · '.mp3'
Resultfalse
fn main() {
println!("{}", "rehearsal.wav".ends_with(".mp3"));
}
false
Fragile shelf labels end in -F. Does this one belong there?
Start'12-F' · '-F'
Resulttrue
fn main() {
println!("{}", "12-F".ends_with("-F"));
}
true