Question:
How to update or add to a u64 value in a sui::Table?
let v = table::borrow_mut(&mut table_with_u64_values, k);
v = v + i;
Gives an error.
Answer:
example:
let v = table::borrow_mut(&mut table_with_u64_values, k);
*v = *v + i;
value: &mut u64
i: u64
The example updates a mutable borrowed value.
docs: “Both mutable and immutable references can be read to produce a copy of the referenced value.
Only mutable references can be written. A write *x = v
discards the value previously stored in x
and updates it with v
.”
Hence the first *v is a write to the table value and the second *v is a dereference and a read of the previously stored value in v.
docs: reading and writing through references
docs: reading and writing fields