for (i, element) in new_vector.iter_mut().enumerate() {
let new_element = array[i as usize] + array[i as usize - 1];
println!("Element {}", *element);
println!("New Element {}", new_element);
println!("Count {}", i);
new_vector[i as usize + 1] = new_element;
}
This doesn’t work due to barrowing twice. The documents are non-existent. Nothing explains something I know is simple. I couldn’t reference it (new_vector[slice]) as just a reference location and edit it either. e.g. below.
How is there no docs on the simplest item. This shouldn’t be an issue as redefining values it like program step 5 and I can’t find an explanation anywhere.
for element in &mut new_vector {
let new_element = array[count as usize] + array[count as usize - 1];
println!("Element {}", element);
println!("New Element {}", new_element);
println!("Count {}", count);
(Either '*' or '&' or any combination)new_vector[count as usize + 1] = new_element;
count = count + 1;
}
Third segment; I assume this means I cannot take “ownership’? Who owns this if the vector doesn’t??? I’d love to go through Rust docs but nothing tells me how to even edit vector values vector[index] = value; I’m also not told how to get any dynamically sized list which is exactly what I need to even do the tutorial. I can find most everything of other languages but I’m submitting this as a doc in case for others in the future.
for element in new_vector {
let new_element = array[count as usize] + array[count as usize - 1];
let mut vector = new_vector;
println!("Element {}", element);
println!("New Element {}", new_element);
println!("Count {}", count);
vector[count as usize + 1] = new_element;
count = count + 1;
}
I was expecting this equivalent in my original program.
I’m told arrays can’t expand but this is the idea. Something in the realm of array list
arrayList arr = [0,1,0,0,0,0];
for (i=1; i < arr.length-1; i++) {
arr[i+1] = (arr[i] + arr[i-1]);
}
End Result = [0,1,1,2,3,5]
This all comes from just getting to Rust ‘Getting Started’ up to doing a Fibonacci.
But nothing expands or allows me to set a value even with what feels like very in-line (not kicking to functions) coding.
Please explain who has or where the ownership goes in code.
BabyLogan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.