Path::ends_with not working

I used a &str and then switched to OsStr, and the code stopped working because ends_with stopped returnig a correct result.

Here's the code that reproduces the problem:

use std::{path::Path, ffi::OsStr};

fn main() {
	let p1 = "test.osm.gz";
	println!("{}", p1.ends_with(".osm.gz"));
	let p2 = OsStr::new("test.osm.gz");
	let p3 = AsRef::<Path>::as_ref(p2);
	println!("{}", p3.ends_with(".osm.gz"));
	println!("{}", p3.ends_with(Path::new(".osm.gz")));
}

it prints true, false, and false (I expected all 3 be true).

Why does this happen?

Because Path methods tend to work on the path component level, not the substring level.

Only considers whole path components to match.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.