@@ -2428,7 +2428,7 @@ use replacer_closure::*;
2428
2428
///
2429
2429
/// # Implementation by closures
2430
2430
///
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`
2432
2432
/// and `'b`) and which return a type `T: AsRef<str>` (that may depend on `'a`
2433
2433
/// or `'b`) implement the `Replacer` trait through a [blanket implementation].
2434
2434
///
@@ -2447,33 +2447,27 @@ use replacer_closure::*;
2447
2447
/// ```
2448
2448
///
2449
2449
/// 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:
2455
2452
///
2456
2453
/// ```
2457
2454
/// use regex::{Captures, Regex};
2458
2455
/// use std::borrow::Cow;
2459
2456
///
2460
- /// fn coerce<F: for<'a> FnMut(&'a Captures<'_>) -> Cow<'a, str>>(f: F) -> F {
2461
- /// f
2462
- /// }
2463
- ///
2464
2457
/// 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);
2475
2466
/// assert_eq!(result, "1234,012345");
2476
2467
/// ```
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.
2477
2471
pub trait Replacer {
2478
2472
/// Appends possibly empty data to `dst` to replace the current match.
2479
2473
///
0 commit comments