More actions
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
but this bit errors | but this bit errors - And this is because the unwrap on line 158 has already consumed the value contained in the <code>Result</code> and assigned it to <code>unit_list</code> so it now doesn't exist. So always read the manual to see if the variable is consumed based on the functions/traits you are using. | ||
<syntaxhighlight lang="rust" line start="156" highlight="4"> | <syntaxhighlight lang="rust" line start="156" highlight="4"> | ||
for unit_item in unit_iter.unwrap() { | for unit_item in unit_iter.unwrap() { | ||
Line 22: | Line 23: | ||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> | ||
[category:Rust_Notes] | [category:Rust_Notes] | ||
Line 48: | Line 50: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[category:Rust_Notes]] |
Latest revision as of 10:59, 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 - 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. So always read the manual to see if the variable is consumed based on the functions/traits you are using.
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(),
| ++++++++