How to change ownership without clone()

Hi, I'm new to Rust so this might be a dumb question. The following code doesn't work because elgamal_key_bytes is 'moved' when it's passed into write() and therefore it cannot be used again in evaluate(). How can I fix this ownership issue without using expensive operations like clone()?

    let elgamal_key_bytes = vec![];
    elgamal_key.write(elgamal_key_bytes);
    
    let h_prev_bytes = vec![];
    let h_prev = <CRH::<ConstraintF<C>, MyPoseidonParams> as TwoToOneCRH>::evaluate(&poseidon_params, &elgamal_key_bytes, &[i_prev]).unwrap();
    h_prev.write(h_prev_bytes);

    let i: u8 = 10;
    let h_cur = <CRH::<ConstraintF<C>, MyPoseidonParams> as TwoToOneCRH>::evaluate(&poseidon_params, &elgamal_key_bytes, &[i]).unwrap();

I don't know the type of elgamal_key, which limits how useful I can be, but you might try

    let mut elgamal_key_bytes = vec![];
    elgamal_key.write(&mut elgamal_key_bytes);

This would be the conventional way to treat a vector like a writeable stream. Vec<u8> implements Write, and &mut W implements Write whenever W implements Write, so &mut Vec<u8> should be able to be written to.

If that doesn't work, show us the types and the actual error.

Oh that worked! Thank you!

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.