I can't get first element of the vector because of the error and can't change struct design either. I tried borrowing but struct expects a ExtrudeGeometry.
#[wasm_bindgen]
pub fn toCollection(arr: js_sys::Array, r_type: String) -> JsValue {
let n_arr: Vec<ExtrudeGeometry> = arr.into_serde().unwrap();
if r_type == "GeometryCollection" {
return JsValue::from_serde(&OutputGeometryCollection {
collection: n_arr,
r#type: r_type,
})
.unwrap();
} else {
let ex: ExtrudeGeometry = n_arr[0];
return JsValue::from_serde(&OutputObject {
data: ex,
r#type: r_type,
})
.unwrap();
}
}
error[E0507]: cannot move out of borrowed content
--> src/lib.rs:308:39
|
308 | let ex: ExtrudeGeometry = n_arr[0];
| ^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider borrowing here: `&n_arr[0]`
I assume in this answer that the ownership system of Rust is known. Your vector owns the items, so if you ask for the first one, you can only borrow it because the vector is composed of items contiguous in memory. You cannot randomly remove an item from it with the index notation.
If you want to take the first one, you have 3 choices:
You don't care about the remaining of the vector: you can transform it into an iterator and take the first item iterated:
vector
.into_iter() // consume the vector to get an iterator
.next() // get the first iterated item
.unwrap()
You care about the remaining, but you don't care about the ordering, use swap_remove
:
vector.swap_remove(0)
You care about the remaining and the ordering: don't use a vector. I you don't have that choice, you can use remove
, but that's a O(n) function.
By the way, the return in last position is not idiomatic:
#[wasm_bindgen]
pub fn toCollection(arr: js_sys::Array, r_type: String) -> JsValue {
let n_arr: Vec<ExtrudeGeometry> = arr.into_serde().unwrap();
if r_type == "GeometryCollection" {
JsValue::from_serde(&OutputGeometryCollection {
collection: n_arr,
r#type: r_type,
})
.unwrap()
} else {
let ex = n_arr.into_iter().next().unwrap();
JsValue::from_serde(&OutputObject {
data: ex,
r#type: r_type,
})
.unwrap();
}
}