Skip to content

Commit 1aab0b8

Browse files
committed
fixup! More generic impl of Replacer for closures
Do not mention unstable features in documentation. Do not include coercing helper function in example code.
1 parent d89b31a commit 1aab0b8

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/regex/string.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,7 @@ use replacer_closure::*;
24282428
///
24292429
/// # Implementation by closures
24302430
///
2431-
/// Closures that take an argument of type `&'a Captures<'b>` (for any `'a`
2431+
/// Closures that take an argument of type `&'a Captures<'b>` (for all `'a`
24322432
/// and `'b`) and which return a type `T: AsRef<str>` (that may depend on `'a`
24332433
/// or `'b`) implement the `Replacer` trait through a [blanket implementation].
24342434
///
@@ -2447,33 +2447,27 @@ use replacer_closure::*;
24472447
/// ```
24482448
///
24492449
/// The return type of the closure may depend on the lifetime of the reference
2450-
/// that is passed as an argument to the closure. Unless [closure lifetime
2451-
/// binders] are being used, the correct type of the closure must be known to
2452-
/// the compiler, e.g. by coercing it through a helper function:
2453-
///
2454-
/// [closure lifetime binders]: https://rust-lang.github.io/rfcs/3216-closure-lifetime-binder.html
2450+
/// that is passed as an argument to the closure. Using a function, this can be
2451+
/// expressed:
24552452
///
24562453
/// ```
24572454
/// use regex::{Captures, Regex};
24582455
/// use std::borrow::Cow;
24592456
///
2460-
/// fn coerce<F: for<'a> FnMut(&'a Captures<'_>) -> Cow<'a, str>>(f: F) -> F {
2461-
/// f
2462-
/// }
2463-
///
24642457
/// let re = Regex::new(r"[0-9]+").unwrap();
2465-
/// let result = re.replace_all(
2466-
/// "1234,12345",
2467-
/// coerce(|caps| {
2468-
/// if caps[0].len() % 2 == 1 {
2469-
/// Cow::Owned(format!("0{}", &caps[0]))
2470-
/// } else {
2471-
/// Cow::Borrowed(&caps[0])
2472-
/// }
2473-
/// }),
2474-
/// );
2458+
/// fn func<'a, 'b>(caps: &'a Captures<'b>) -> Cow<'a, str> {
2459+
/// if caps[0].len() % 2 == 1 {
2460+
/// Cow::Owned(format!("0{}", &caps[0]))
2461+
/// } else {
2462+
/// Cow::Borrowed(&caps[0])
2463+
/// }
2464+
/// }
2465+
/// let result = re.replace_all("1234,12345", func);
24752466
/// assert_eq!(result, "1234,012345");
24762467
/// ```
2468+
///
2469+
/// *Note:* Using a closure instead of a function in the last example can be
2470+
/// more tricky and requires a coercing helper function as of yet.
24772471
pub trait Replacer {
24782472
/// Appends possibly empty data to `dst` to replace the current match.
24792473
///

0 commit comments

Comments
 (0)