Rust thinks I'm letting a reference escape

⚓ Rust    📅 2025-10-22    👤 surdeus    👁️ 3      

surdeus

error[E0521]: borrowed data escapes outside of method
  --> src/event.rs:21:9
   |
20 |     fn add_assign(&mut self, rhs: &dyn Fn(Sig)->()) {
   |                              ---  - let's call the lifetime of this reference `'1`
   |                              |
   |                              `rhs` is a reference that is only valid in the method body
21 |         self.listeners.push(MutRc::new(rhs));
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |         |
   |         `rhs` escapes the method body here
   |         argument requires that `'1` must outlive `'static`
   |
   = note: requirement occurs because of a mutable reference to `Vec<MutRc<dyn Fn(Sig)>>`
   = note: mutable references are invariant over their type parameter
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

Its not escaping because my MutRc<T> is a mutable reference counter.

MutRc source (click for more details)

3 posts - 3 participants

Read full topic

🏷️ Rust_feed