More actions
No edit summary |
m Sharpic moved page Rust Notes to Consumption without leaving a redirect: Already Have Catagory |
||
(No difference)
|
Revision as of 10:57, 12 February 2025
Consumption
This is a critically important concept and variable error and disapear even though you think they shouldnt because it depends if the function or trait consumes them.
This bit of code works
for unit_item in unit_iter.unwrap() {
let unit = unit_item.unwrap();
unit_list.insert(unit.unit_code.clone(), unit);
}
but this bit errors
for unit_item in unit_iter.unwrap() {
unit_list.insert(
unit_item.unwrap().unit_code.clone(),
unit_item.unwrap().clone(),
);
}
[category:Rust_Notes]
--> src/unit_management.rs:159:17
|
156 | for unit_item in unit_iter.unwrap() {
| --------- move occurs because `unit_item` has type `Result<UnitType, rusqlite::Error>`, which does not implement the `Copy` trait
157 | unit_list.insert(
158 | unit_item.unwrap().unit_code.clone(),
| --------- -------- `unit_item` moved due to this method call
| |
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
159 | unit_item.unwrap().clone(),
| ^^^^^^^^^ value used here after move
|
note: `Result::<T, E>::unwrap` takes ownership of the receiver `self`, which moves `unit_item`
--> /home/sharpic/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1098:19
|
1098 | pub fn unwrap(self) -> T
| ^^^^
help: you could `clone` the value and consume it, if the `rusqlite::Error: Clone` trait bound could be satisfied
|
158 | unit_item.clone().unwrap().unit_code.clone(),
| ++++++++
And this is because the unwrap on line 158 has already consumed the value contained in the Result
and assigned it to unit_list
so it now doesn't exist.