How do I properly resolve the error "temporary value dropped while borrowed"?

Hello! I'm very new to rust but I've been using the zama-concrete rust crate for a homomorphic encryption project.

I have a vector of ciphertexts (type FheUint16) that I want to find the sum of. vec.iter().sum() didn't work for this data type so I resorted to using a for loop. However I keep running into the error "temporary value dropped while borrowed".

Any temporary solutions I've tried don't give me the wanted behavior either. How do I properly keep updating the "sum" variable? I've attached some photos for reference. Thank you for your help!

image

Here's a photo of the error message:

let mut sum = input[0].clone
...
sum = sum + ct

Incidentally, we post formatted code and error blocks as text on this forum, not images (modulo things like IDE help).

1 Like

Rust references are not for storing objects "by reference". Rust references are only a temporary permission to view an existing value already stored somewhere else.

It's not possible to make a reference to a newly-created temporary result. The value has to be stored somewhere first. (sum + ct) creates a new value, and you can't borrow this object when it's not stored anywhere. You need to store result of (sum + ct) to be able to make a reference (a temporary view) of the location where it's stored.

In your case you should just avoid trying to use references for sum. This is a new value that you're creating/modifying, not a read-only view of a value that already existed before.

2 Likes

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.