← Back to context

Comment by IshKebab

1 day ago

How is that different to

  Fn(_: &mut T)

?

In the former the caller does not retain access to T until Fn returns.

  • I think I'm lost. If I give a mutable reference to a function... I can't access it (even read it) until it returns, no?

    What is different?

    • Let's say a function "foo" calls "fn bar(_: &mut T) -> ()".

      When passing a mutable reference, the lifetime of the object is largely decided by "foo" (with some caveats).

      Now, let's say that "foo" instead calls "fn bar(_: T) -> T".

      When passing the object itself, the lifetime is largely decided/decide-able by "bar".

  • That's true of mutable references too though isn't it? In fact lots of people have suggested they should really have been called "exclusive references", since you can actually mutate some objects through non-exclusive references (called "interior mutability" normally).