Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why Path.ends_with return false when passed a correctly-matching file extension?

Tags:

rust

why this code return false

use std::path::Path;
fn main() {
    println!(
        "Ends with? {:?}",
        &Path::new("some.file.d.ts").ends_with("ts")
    );
}

playground link

like image 474
neuronet Avatar asked Sep 18 '25 13:09

neuronet


1 Answers

The documentation is explicit about this:

Determines whether child is a suffix of self.

Only considers whole path components to match

An extension like ts is not a whole path component.


So, a case where it would be true would be something like:

use std::path::Path;
fn main() {
    println!(
        "Ends with? {:?}",
        &Path::new("/path/to/some.file.d.ts").ends_with("some.file.d.ts")
    );
}
like image 194
Charles Duffy Avatar answered Sep 20 '25 06:09

Charles Duffy