Yes I have read the docs, three times.
I have looked at: Cannot move out of borrowed content in Rust and Cannot move out of borrowed content. Neither of these answer my question or help me get a better understanding of what's going on here.
Consider the following code snippet:
pub trait Scene {
fn on_start(&mut self) {}
fn update(&mut self) {}
fn stop(&mut self) {}
fn is_active(&self) -> bool { return false; }
}
pub struct SceneManager {
scenes: Vec<Box<Scene>>
}
impl SceneManager {
fn new<T>(mut scene: T) -> SceneManager where T: Scene + 'static {
scene.on_start();
SceneManager {
scenes: vec![Box::new(scene)]
}
}
fn update_children(&mut self) {
for mut scene in &mut self.scenes {
scene.update();
}
}
...
fn get_children(&self) -> Vec<Box<Scene>> {
return self.scenes
}
...
}
#[cfg(test)]
mod tests {
use super::*;
struct Sample {
running: bool
}
impl Scene for Sample {
fn update(&mut self) {
if self.running {
self.stop()
}
}
fn stop(&mut self) {
self.running = false;
}
fn is_active(&self) -> bool { self.running }
}
...
#[test]
fn test_no_scene_is_running() {
let mut scene_manager = SceneManager::new(Sample{running: true});
scene_manager.update_children();
let children = scene_manager.get_children();
for mut scenes in children {
assert!(!scene.is_active());
}
}
}
Issue:
error[E0507]: cannot move out of borrowed content
--> src/main.rs:12:9
|
12 | self.scenes
| ^^^^ cannot move out of borrowed content
We have seen it a million times. Most of the answers say to use &self
instead of self
. Well that's what I am doing here. even &mut self
doesn't work, although I am not mutating anything here so no need for that.
Please enlighten me and point me to docs that explain how I cannot do this.