[Solved] Issue with a Higher-Ranked Trait Bound

Hi all,

This post is a followup to this one.

I'm trying to create a function that inserts a &str before a Pattern.

Here's a working example:

#![feature(pattern)]

use std::str::pattern::{Pattern};

fn insert_before<P>(input: &mut String, pattern: P) 
    where for<'a> P: Pattern<'a>,
{
    if let Some(index) = input.find(pattern) {
        input.insert_str(index, "|");
    }
}

This code compiles, but as soon as I change find with rfind I get a compile error :

the trait bound `<P as Pattern<'_>>::Searcher: ReverseSearcher<'_>` is not satisfied

Here's the failing example:

#![feature(pattern)]

use std::str::pattern::{Pattern};

fn insert_before<P>(input: &mut String, pattern: P) 
    where for<'a> P: Pattern<'a>,
{
    if let Some(index) = input.rfind(pattern) {
        input.insert_str(index, "|");
    }
}

I tried to follow the compiler suggestions like this:

#![feature(pattern)]

use std::str::pattern::{Pattern, ReverseSearcher};

fn insert_before<P>(input: &mut String, pattern: P) 
    where for<'a> P: Pattern<'a>,
    for<'a> <P as Pattern<'a>>::Searcher: ReverseSearcher<'a> // I tried to this bound
{
    if let Some(index) = input.rfind(pattern) {
        input.insert_str(index, "|");
    }
}

But without success:

error[E0277]: the trait bound `for<'a> <_ as Pattern<'a>>::Searcher: ReverseSearcher<'a>` is not satisfied

Is this related to Higher-Ranked Trait Bound like in the previous post ?
How can I satisfy the trait bound ?

Thank you for your help.

Looks like some sort of normalization problem to me. There might be a way to work around it with your own helper traits (but I haven't tried yet).

Here's a workaround (that could use more tests).

1 Like

The second on looks good.

Thank you @quinedo!

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.