0

I am assigning data to properties of a struct, but the following error appears:

error[E0382]: borrow of partially moved value: `super_hero`
   --> src/main.rs:16:22
    |
13  |     for mut chunk in super_hero.description_chunks.unwrap() {
    |                                                    -------- `super_hero.description_chunks` partially moved due to this method call
...
16  |     println!("{:?}", &super_hero);
    |                      ^^^^^^^ value borrowed here after partial move
    |
note: this function takes ownership of the receiver `self`, which moves `super_hero.description_chunks`
   --> /Users/someuser/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:752:25
    |
752 |     pub const fn unwrap(self) -> T {
    |                         ^^^^
    = note: partial move occurs because `super_hero.description_chunks` has type `std::option::Option<Vec<Chunk>>`, which does not implement the `Copy` trait
help: consider calling `.as_ref()` to borrow the type's contents
    |
13  |     for mut chunk in super_hero.description_chunks.as_ref().unwrap() {
    |                                                   +++++++++

The following is the code where I am assigning data to properties of a struct:

let super_hero_description = "several years ago.";
    let mut super_hero = SuperHero::new(super_hero_description);
    super_hero.description_chunks = Option::from(super_hero.get_decription_chunks(DescriptionTypes::NotVillan));
    for mut chunk in super_hero.description_chunks.unwrap() {
        chunk.data = Option::from(chunk.get_data(DescriptionTypes::NotVillan).await);
    }
    println!("{:?}", &super_hero);

The error does not happen until I try to print the super_hero at the end.

Oh, and if I follow the recommendation rust gives me

for mut chunk in super_hero.description_chunks.as_ref().unwrap() {
    |                                                   +++++++++

I end up with another error:

error[E0594]: cannot assign to `chunk.data`, which is behind a `&` reference
  --> src/main.rs:14:9
   |
13 |     for mut subdivision in super_hero.description_chunks.as_ref().unwrap() {
   |                               ------------------------------------- this iterator yields `&` references
14 |         chunk.data = Option::from(chunk.get_data()(DescriptionTypes::NotVillan).await);
   |         ^^^^^^^^^^^^ `chunk` is a `&` reference, so the data it refers to cannot be written
Xavi Font
  • 296
  • 4
  • 19
  • Does this answer your question? [Reference to unwrapped property fails: use of partially moved value: `self`](https://stackoverflow.com/questions/40615054/reference-to-unwrapped-property-fails-use-of-partially-moved-value-self) – Chayim Friedman Jun 29 '22 at 22:36
  • @ChayimFriedman nope because they are unwrapping a reference in that example, here I am unwrapping an owned value. But thanks anyway. – Xavi Font Jun 29 '22 at 22:41

1 Answers1

1

rustc is correct, it just needs a little adjustment: instead of as_ref() use as_mut(), that converts &mut Option<T> to Option<&mut T> instead of &Option<T> to Option<&T>.

The problem is that unwrap() requires an owned Option: see Reference to unwrapped property fails: use of partially moved value: `self`.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • Yes! Thanks, I need to read that for sure. (I cannot accept the answer for the next six minutes... you were too quick). – Xavi Font Jun 29 '22 at 22:43