I'm new to Rust and toying a bit with it. It's my first program, and it seems I've already encountered the dreaded borrow checker. :)
pub struct Foo<T> {
memory: Vec<T>
}
impl<T> Foo<T> { {
pub fn set(&mut self, value: T) {
self.memory.push(value);
}
pub fn get(&self, index: usize) -> Option<&T> {
Some(&self.memory[index])
}
}
It compiles just fine but I want to return value not reference from get function.
If I do
pub fn get(&self, index: usize) -> Option<T> {
Some(*&self.memory[index])
}
Which fails with:
error: cannot move out of borrowed content [E0507]
Some(*&self.memory[index])
I just have no idea why the borrow checker behaves this way.
How can I return the value? Can anyone enlighten me?
Rem: It's not duplicate question. I don't ask someone explain what "indexed content" means but how to return value with no cannot move out of borrowed content
error.