Skip to content

Lifetime confusion around replace_all closures. #775

Answered by BurntSushi
tpoliaw asked this question in Q&A
Discussion options

You must be logged in to vote

NOTE: See @sgrif's comments below and their PR in #776 for the actual answer. I also created #777 to track this particular issue, since it can't be fixed until a hypothetical regex 2.0.0 release.

I believe the issue rests in how Replacer is implemented for closure types:

impl<F, T> Replacer for F
where
    F: FnMut(&Captures<'_>) -> T,
    T: AsRef<str>,
{
    fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
        dst.push_str((*self)(caps).as_ref());
    }
}

I think the key part is this:

    F: FnMut(&Captures<'_>) -> T,
    T: AsRef<str>,

That is, there is nothing connecting the lifetime of '_ in &Captures<'_> with T. So in a generic context, I guess that the com…

Replies: 2 comments 8 replies

Comment options

You must be logged in to vote
8 replies
@sgrif
Comment options

@sgrif
Comment options

@sgrif
Comment options

@tpoliaw
Comment options

@BurntSushi
Comment options

Answer selected by BurntSushi
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants