You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When attempting to call replace_all on a Regex with a str as the argument for rep(as opposed to a NoExpand), the Replacer trait implementation for str is apparently being ignored, instead falling back to the FnMut implementation. This project demonstrates the following compile error:
src\main.rs:13:23: 13:53 error: the trait `for<'r,'r> core::ops::Fn<(&'r regex::re::Captures<'r>,)>` is not implemented for the type `str` [E0277]
src\main.rs:13 true => regex.replace_all(&source, &replace.as_str()),
Where replace and source are both Strings.
Edit: just fixed the type of replace by adding as_str, as the & was not coercing the String to a str properly. See the latest commit.
The text was updated successfully, but these errors were encountered:
If you use replace.as_str() instead of &replace.as_str(), then your code works. The as_str method returns a &str, so &replace.as_str() has type &&str, and no impl for that exists.
I wonder if we can add a impl<'a, T: Replacer> &'a T for Replacer { ... } impl that would fix this. The standard library uses this convention AFAIK.
BurntSushi
changed the title
Replacer trait for str is being ignored to determine type info for replace argument on replace_all calls
implement &T for Replacer where T: Replacer
Jun 19, 2015
I feel like it's probably not worth adding this impl unless there's a more solid reason. (It could be added in a backwards compatible manner later if we really wanted to, I think.)
This permits use of a Replacer without consuming it.
Note: This can't simply return `&mut Self` because a generic
`impl<R: Replacer> Replacer for &mut R` would conflict with libstd's
generic `impl<F: FnMut> FnMut for &mut F`.
See also: #83Closes#449
When attempting to call replace_all on a Regex with a str as the argument for rep(as opposed to a NoExpand), the Replacer trait implementation for str is apparently being ignored, instead falling back to the FnMut implementation. This project demonstrates the following compile error:
Where replace and source are both Strings.
Edit: just fixed the type of replace by adding as_str, as the & was not coercing the String to a str properly. See the latest commit.
The text was updated successfully, but these errors were encountered: