0

I have this code (PLAYGROUND HERE):

#[derive(Debug, Default, Clone)] pub struct BaseFile { pub id: Option<String>, pub location: String, pub size: u64, } impl BaseFile { pub fn filename(&self) -> String { "CALCULATED_FILENAME".to_string() } } #[derive(Debug, Default, Clone)] pub struct AdvancedFile { pub id: Option<String>, pub filename: String, pub size: u64, } impl AdvancedFile { pub fn from_base( id: Option<String>, filename: String, size: u64, ) -> Self { Self { id, filename, size, } } } impl From<BaseFile> for AdvancedFile { fn from(base_file: BaseFile) -> Self { Self::from_base( base_file.id, base_file.filename(), base_file.size, ) } } #[tokio::main] async fn main() { let domain_file = BaseFile { id: None, location: "new_location".to_string(), size: 123, }; dbg!(domain_file); } 

with this error:

error[E0382]: borrow of partially moved value: `base_file` --> src/main.rs:39:13 | 38 | base_file.id, | ------------ value partially moved here 39 | base_file.filename(), | ^^^^^^^^^^^^^^^^^^^^ value borrowed here after partial move | = note: partial move occurs because `base_file.id` has type `Option<String>`, which does not implement the `Copy` trait 

Why this error if I'm using &self in pub fn filename(&self) -> String {?

1 Answer 1

3

Since that object is being ripped apart and reassembled, you may need to capture the parts before you do that:

// Capture any values that depend on `base_name` being intact let filename = base_name.filename(); Self::from_base( base_file.id, filename, base_file.size, ) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.