r/rust•
metadeus

Destructuring problem

pub struct ModelInfo {
    pub obj_reg: ObjReg, // <- struct typre
    pub metainfo: Box<MetaInfoProvider>, // <- box of a trait
    pub obj_pool: ObjPool, // <- struct type
    pub str_int: StrInt, // <- struct type
}
impl ModelInfo {
pub fn merge(mut self, o: Self) -> Self {
    let Self { mut str_int, mut obj_reg, mut metainfo, mut obj_pool } = o;
    for (sym, s) in str_int.into_iter() { // into_iter consumes self
    }
    self
}
}

How is this even possible?

error[E0507]: cannot move out of borrowed content
    |
201 |         for (sym, s) in str_int.into_iter() {
    |                         ^^^^^^^ cannot move out of borrowed content

As I understand, content is not borrowed but moved here. What am I missing?

Posted Apr 23, 2018, 3:56 PM UTC · edited

29
steveklabnik1rust•

What are all the types here? any chance you can give a playpen showing the error?

3
metadeusOP•

I've added the struct type. Unfortunately, it is a part of a huge app and I can't disclose the source code. I thought that it is maybe kind of an obvious mistake.

1
steveklabnik1rust•

It happens. I, at least, won't be able to sort it without the types.

https://play.rust-lang.org/?gist=b1b197e553d9b56b17fbd6e5dab0944b&version=stable

What's the intoiterator look like?

2
metadeusOP•

Oh, I think I found the problem, this code involves Deref and it returns a reference. Thank you!

2
steveklabnik1rust•

Great!

3
mikeyhew•

Ah, so StrInt doesn't implement IntoIterator, but derefs to something that does?

2
[deleted]•

[deleted]

1
eddyb•

My guess is that str_int is a reference - can you try without the .into_iter() method call?

2
metadeusOP•

But why is it a reference and how to make it a value there?

1
eddyb•

I was guessing that ModelInfo's str_int contained a reference to StrInt does, but I was wrong, sorry.

1