And I want to create a Vector of the struct field Position, so node -> Vec.
For that I have the following:
let mut path = HashSet::new();
let mut current_node = Rc::new(exit_node);
while current_node.parent != None {
let parent = current_node.parent.expect("");
path.insert(parent.position.clone());
current_node = parent;
}
This is the compiling output:
27 | let parent = current_node.parent.expect("");
| ^^^^^^^^^^^^^^^^^^^
| |
| move occurs because value has type `std::option::Option<std::rc::Rc<algorithms::Node>>`, which does not implement the `Copy` trait
| help: consider borrowing the `Option`'s content: `current_node.parent.as_ref()`
it's more idiomatic (and potentially slightly faster) to write:
while let Some(x) = x {
}
As for expect/unwrap complaining about move: unwrapping is a self-destruct operation for the Option. If you don't own the option, you can't destroy it.
There's as_ref() and as_deref() that flips a borrowed option inside-out, so instead of a borrowed option of an owned value, you get to own option of a borrowed value. This lets you unwrap and destroy the Option.