Is this a borrow checker bug?

I'm new to rust and was wondering if I am using generics wrong here or if this is a bug with the borrow checker. As far as I can tell it seems like a bug with the borrow checker since the output of the function does not included what the compiler is complaining about.

If I make the function take in list as &[&str] it compiles fine

You'll need to change this where clause:

    &'a str: std::borrow::Borrow<T>,

to this:

    for<'x> &'x str: std::borrow::Borrow<T>,
4 Likes

Pretext: function generics are chosen by the caller, and in particular lifetime generics...

pub fn does_dir_contain<'a, T>(
//                      ^^

...are always longer than the function body, because the caller must choose something valid for the entire function call. Or another way to think about it is, the caller could have chosen 'static.

Then to hopefully explain:

You need some &'? str: Borrow<T> bound to call str_names.contains, because str_names is a HashSet<&str> (for some lifetime). But with the &'a str: Borrow<T> bound, it will only work if your str_names is a HashSet<&'a str> exactly. In turn, that can only be the case if you borrowed your local file_names for 'a, which you can't do -- that caller-chosen lifetime is too long.

You need a &'some_local_borrow str: Borrow<T> bound, and the way you express that is with a higher-ranked trait bound (HRTB) like @mbrubeck said:

for<'x> &'x str: std::borrow::Borrow<T>,
2 Likes

Thanks for the explanation. Yeah I was confused I thought the compiler wanted the &'? str: Borrow<T> to define the lifetime of the type inside the slice [T]. It makes more sense it was actually talking about the lifetime of str_names

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.