@@ -2444,47 +2444,31 @@ use replacer_closure::*;
2444
2444
/// assert_eq!(result, "[number with 4 digits],[number with 5 digits]");
2445
2445
/// ```
2446
2446
///
2447
- /// Note that the return type of the closure may depend on the lifetime of the
2448
- /// reference that is passed as an argument to the closure. This requires the
2449
- /// closure to be a function, unless [closure lifetime binders] are being used:
2447
+ /// The return type of the closure may depend on the lifetime of the reference
2448
+ /// that is passed as an argument to the closure. Unless [closure lifetime
2449
+ /// binders] are being used, the correct type of the closure must be known to
2450
+ /// the compiler, e.g. by coercing it through a helper function:
2450
2451
///
2451
2452
/// [closure lifetime binders]: https://rust-lang.github.io/rfcs/3216-closure-lifetime-binder.html
2452
- /// [`Cow`]: std::borrow::Cow
2453
2453
///
2454
2454
/// ```
2455
2455
/// use regex::{Captures, Regex, Replacer};
2456
2456
/// use std::borrow::Cow;
2457
2457
///
2458
- /// let re = Regex::new(r"[0-9]+").unwrap();
2459
- /// fn prepend_odd<'a>(caps: &'a Captures<'_>) -> Cow<'a, str> {
2460
- /// if caps[0].len() % 2 == 1 {
2461
- /// Cow::Owned(format!("0{}", &caps[0]))
2462
- /// } else {
2463
- /// Cow::Borrowed(&caps[0])
2464
- /// }
2458
+ /// fn coerce<F: for<'a> FnMut(&'a Captures<'_>) -> Cow<'a, str>>(f: F) -> F {
2459
+ /// f
2465
2460
/// }
2466
- /// let result = re.replace_all("1234,12345", prepend_odd);
2467
- /// assert_eq!(result, "1234,012345");
2468
- /// ```
2469
- ///
2470
- /// The same example using closure lifetime binders:
2471
- ///
2472
- /// ```ignore
2473
- /// #![feature(closure_lifetime_binder)]
2474
- ///
2475
- /// use regex::{Captures, Regex, Replacer};
2476
- /// use std::borrow::Cow;
2477
2461
///
2478
2462
/// let re = Regex::new(r"[0-9]+").unwrap();
2479
2463
/// let result = re.replace_all(
2480
2464
/// "1234,12345",
2481
- /// for<'a, 'b> |caps: &'a Captures<'b>| -> Cow<'a, str> {
2465
+ /// coerce( |caps| {
2482
2466
/// if caps[0].len() % 2 == 1 {
2483
2467
/// Cow::Owned(format!("0{}", &caps[0]))
2484
2468
/// } else {
2485
2469
/// Cow::Borrowed(&caps[0])
2486
2470
/// }
2487
- /// },
2471
+ /// }) ,
2488
2472
/// );
2489
2473
/// assert_eq!(result, "1234,012345");
2490
2474
/// ```
0 commit comments