# Abstract return types (impl Trait) that return references

**URL:** <https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063>\
**Category:** language design\
**Created:** [June 27, 2023, 5:47am UTC](https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063 "2023-06-27T05:47:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnScience](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/johnscience/32/8970_2.png) [@JohnScience](https://internals.rust-lang.org/u/JohnScience)\
**Post date:** [June 27, 2023, 5:47am UTC](https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063/1 "2023-06-27T05:47:12Z")

</div>

Let's say we want to return from a function a closure that...

1. captures some owned value, e.g. `PathBuf` or `String`, and therefore is a `move` closure.
2. accepts no parameters, i.e. the signature of this closure is `()->...`.
3. can be called multiple times, i.e. it is `Fn() -> ...` closure.
4. returns a shared reference to the captured value that does not outlive the closure and, consequently, the captured variable.

I don't think there's a way to do it using either stable or nightly rust.

I would like to be able to write something like this...

```rust
fn path_to_example_nifti() -> impl Fn() -> &str {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    // ...
    move || path.to_str().unwrap()
}

```

I understand that it's a limitation of `Fn*` traits but I still would love to be able to do something like this.

Learn more about abstract return types:

- [Abstract return types, aka `impl Trait`](https://www.ncameron.org/blog/abstract-return-types-aka--impl-trait-/)

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [June 27, 2023, 6:50am UTC](https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063/2 "2023-06-27T06:50:11Z")

</div>

I don't think this directly involves `impl Trait`, but just `Fn` traits (and what closures desugar to). It has been discussed a couple of times in the past in [Baby Steps](https://smallcultfollowing.com/babysteps/blog/2023/05/09/giving-lending-and-async-closures/) and briefly in [Closures that can return references to captured variables](https://internals.rust-lang.org/t/closures-that-can-return-references-to-captured-variables/14756). It can't be retrofitted in the `Fn*` traits, but it should be possible to make new traits with the property you want (and maybe make them supertraits of the current `Fn*` traits?). The hard part is probably how to handle lifetime inference such that this doesn't break existing code. For now you're probably better off with a separate trait.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 27, 2023, 7:28am UTC](https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063/3 "2023-06-27T07:28:18Z")

</div>

A "how can I do this today" question is a better fit for [the users forum](https://users.rust-lang.org/). The internals forum is better fit for "how can we improve doing this in the future."

For the purpose of future extensions, it's _theoretically_ possible that `Fn` and `FnMut` could be GAT-ified. However, since they also imply that a closure is also `FnOnce`, it's not really possible for them to be used directly; there's no way for `FnOnce` to return borrowed state. New traits are a possibility, but lifetime inference will still be a hard problem.

For right now, since your desired shape is `fn(&self) -> &T`, it's interesting to note that this is the shape `Deref` takes. You should be able to define a type along the lines of

```rust
struct Lend<T, F, O: ?Sized>(T, F)
where
    F: Fn(&T) -> &O;

impl<T, F, O: ?Sized> Deref for Lend<T, F, O>
where
    F: Fn(&T) -> &O,
{
    type Target = O;
    fn deref(&self) -> &O {
        (self.1)(&self.0)
    }
}

```

which allows you to make an adhoc `Deref` item able to return references to the first "state capture" part, using it like

```rust
pub fn path_to_example_nifti() -> impl Deref<Target = str> {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    // ...
    Lend(path, move |path| path.to_str().unwrap())
}

```

and this spelling appears to work, but note that it's very easy when trying to do something like this to encounter confusing higher-ranked lifetime errors, because closures aren't great at `for<'a>` use cases (implicitly introduced here by lifetime elision).

(For example: it's possible to define `Lend` without the `O` type parameter or the `F: Fn` bound, which I did originally since leaving the struct minimally bound is typically preferable. However, while the `Deref` impl still works, attempting to construct one a) requires an extra type annotation on the closure parameter and b) causes a (higher-ranked) lifetime error, potentially due to the presence of the type annotation, unlike when the bound is present structurally. It's very rarely clear what the compiler wants in these cases, and it takes some educated guessing to get a shape the compiler is happier about.)

The maximally general version of this kind of self-borrowing pattern is [yoke](https://lib.rs/crates/yoke), but it's extremely overkill unless you're in the very specific case that it was designed to support (zero-copy deserialization of complex types). You know if you need it (and will probably end up wanting it before you actually need it).

---

<div class="post-metadata">

**Author:** ![gdennie](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gdennie/32/8328_2.png) [@gdennie](https://internals.rust-lang.org/u/gdennie)\
**Post date:** [June 30, 2023, 9:43am UTC](https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063/4 "2023-06-30T09:43:47Z")

</div>

Doesn't the lifetime of a function end when the function returns making the `Fn() -> &str` desire untenable. Perhaps a co-routine akin to `await` or a `yield` statement that pauses execution is what is desired.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 1, 2023, 1:31am UTC](https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063/5 "2023-07-01T01:31:46Z")

</div>

In theory it's possible, because the signature of `Fn() -> &T` actually looks somewhat like `fn call(&self) -> &T`. It just also looks like `fn call_once(self) -> &T`, so it can't be retrofit to the existing `Fn` traits\[1\].

Instead, it'd require separate traits, e.g.

```rust
trait FnLendMut<Args> {
    type Output<'a> where Self: 'a;
    fn call_lend_mut(&mut self) -> Self::Output<'_>;
}

trait FnLend<Args>: FnLendMut<Args> {
    fn call_lend(&self) -> Self::Output<'_>;
}

```

which you could write today (but would be easier to write specialized for the desired signature instead of generic).

* * *

1. 🤓👆 without some undesigned and highly theoretical type shenanigans such that `FnMut: FnOnce` iff the associated `Output` type is not dependent on the GAT input lifetime provided at invoke time.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 29, 2023, 1:32am UTC](https://internals.rust-lang.org/t/abstract-return-types-impl-trait-that-return-references/19063/6 "2023-09-29T01:32:12Z")

</div>

Closed on September 29, 2023, 1:32am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
