# Conditional regex replacement

**URL:** <https://users.rust-lang.org/t/conditional-regex-replacement/97174>\
**Category:** help\
**Created:** [July 19, 2023, 7:11am UTC](https://users.rust-lang.org/t/conditional-regex-replacement/97174 "2023-07-19T07:11:27Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![quinedot](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/quinedot/32/17693_2.png) [@quinedot](https://users.rust-lang.org/u/quinedot)\
**Post date:** [July 20, 2023, 6:04am UTC](https://users.rust-lang.org/t/conditional-regex-replacement/97174/20 "2023-07-20T06:04:06Z")

</div>

> [@jbe](#):
>
> It seems like with a lot of effort, it's possible to let Rust do what I intended to do.

I only skimmed this, but I think you're basically talking about ["I want a trait bound for closures that might or might not borrow their inputs".](https://users.rust-lang.org/t/problems-matching-up-lifetimes-between-various-traits-and-closure-parameters/71994/7) That's the more complicated version, this one would be

```rust
for<'any> F: FnOnce<(&'any Outer,)>,
for<'any> <F as FnOnce<(&'any Outer,)>::Output: AsRef<str>,

```

But you can't use `FnOnce(&Outer) -> R` for this because you're forced to name the return type and we don't have generic type construction parameters (or higher-kinded types or whatever). So then on stable you make your own trait with some blanket implementations etc... but probably run into heaps of inference issues and/or normalization issues.

So it's sometimes possible, but not always practical, and almost always at a cost.

> [@BurntSushi](#):
>
> This is a lot trickier than it sounds, because even if you think you know how to do it, how much more complicated have you made it for the simple use cases that cover 99% of what people need? Because if you've made that harder, then that a design I would reject. And at some point, you have to pop up a level and consider the complication of the API versus the code you're actually saving someone from writing.

💯 \[1\]

* * *

1. but I can be suckered into writing the complicated version (or at least re-explaining it) when it comes up on this forum more often than not 😅

---

_[View the full topic](https://users.rust-lang.org/t/conditional-regex-replacement/97174)._
