J'essaie d'emprunter de manière mutable une variable mutable. Deref
et DerefMut
sont mises en œuvre pour Foo
, mais échoue compilation:Pourquoi un emprunt mutable d'une fermeture via DerefMut ne fonctionne pas?
use std::ops::{Deref, DerefMut};
struct Foo;
impl Deref for Foo {
type Target = FnMut() + 'static;
fn deref(&self) -> &Self::Target {
unimplemented!()
}
}
impl DerefMut for Foo {
fn deref_mut(&mut self) -> &mut Self::Target {
unimplemented!()
}
}
fn main() {
let mut t = Foo;
t();
}
error[E0596]: cannot borrow immutable borrowed content as mutable
--> src/main.rs:20:5
|
20 | t();
| ^cannot borrow as mutable
't.deref_mut()()' œuvres. Intéressant. –