Consider this function that should return the file extension of a given Path.
pub fn get_extension<\
This isn't a bug. The "problem" here is as_slice's definition. It takes a reference to its arguments, and returns a &str with the same lifetime as the reference, it can't introspect into the internal lifetimes of whatever type it is being called on. That is, path_str.as_slice() returns a &str that lasts for as long as path_str, not as long as the data path_str points at (the original Path).
In other words, there's two lifetimes here. I'll use a hypothetical block-lifetime annotation syntax on the example from @Arjan's filed bug (this answer is based of my response there).
fn test<'a>(s: &'a String) -> &'a str {
'b: {
let slice: &'a str = s.as_slice();
slice.as_slice()
}
}
For the second as_slice call we have self: &'b &'a str, and thus it returns &'b str, which is too short: 'b is just local to test.
As you discovered, the fix now is just removing the extraneous as_slice call. However, with dynamically sized types (DST), we will be able to write impl StrSlice for str, and then slice.as_slice() will be returning a &'a str, since there won't be an extra layer of references (that is, self: &'a str).