Avatar ·

Managing garbage collector lifetime in Rust: how to properly free memory?

Hello everyone! I'm new to Rust, coming from Java where everything is simple — the GC cleans up memory by itself. In Rust, I've encountered the concept of ownership and borrowing, and I'm having a problem.\n\nI'm writing a small game where there are objects that need to live for an indefinite amount of time. For example, bullets, enemies, effect particles. In Java, I would just create `new Bullet()`, and the GC would delete them when they go out of scope. In Rust, the compiler complains about lifetimes.\n\nHere's simplified code that doesn't compile:\n\n```rust\nstruct Bullet {\n x: f32,\n y: f32,\n alive: bool,\n}\n\nstruct Game {\n bullets: Vec,\n}\n\nimpl Game {\n fn spawn_bullet(&mut self, x: f32, y: f32) {\n // Error: cannot return reference to self.bullets\n let bullet = Bullet { x, y, alive: true };\