How to return an owned property from an API response?

I would like to write a function that uses the aws s3 api to list the contents of a bucket (a list of ListBucketResponse) and return just the contents field from the first element for (immutable) use elsewhere in the application. Currently I get the error:

error[E0507]: cannot move out of a shared reference
  --> src\
99 |     list_result.first().unwrap().contents
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `Vec<Object>`, which does not implement the `Copy` trait

from the following code:

async fn get_bucket_contents
    (s3_client: Bucket) -> Vec<Object> 

    let list_result = s3_client
        .list("".to_string(), None)
        .expect("failed to list s3 contents");


Why is the list_result reference shared? I thought it would be owned entirely by this function and could transfer ownership of a child property when the function terminates.

thank you all in advance for your help!

.first() temporarily borrows the first element, without ability to move or return it elsewhere. Rust references allow looking at elements, but not own or destroy them.

vec.remove(0) or vec.swap_remove(0) will remove the element from a vector, and pass ownership of the element to you, so then you can use that element however you want.

Alternatively, .clone() an element to get a new copy that you can return.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.