Issue
As the title says I am trying to iterate through a HashMap of 2 vectors. my code is as follows:
pub fn serialize_hashmap(data: &HashMap<Vec<u8>, Vec<u8>>) -> Result<Vec<u8>, String> {
let mut serialized_data = Vec::new();
for (hash, password_data) in &data {
serialized_data.extend_from_slice(&hash);
let password_data_length: u64 = password_data.len().try_into().unwrap();
serialized_data.write_u64::<NativeEndian>(password_data_length).unwrap(); // TODO add error handling
serialized_data.extend_from_slice(&password_data);
}
return Ok(serialized_data);
}
But when I run this code I get the following error:
error[E0277]: the size for values of type `[_]` cannot be known at compilation time
--> src/main.rs:8:10
|
8 | for (hash, password_data) in &data {
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[_]`
= note: all local variables must have a statically known size
= help: unsized locals are gated as an unstable feature
error[E0277]: the size for values of type `[_]` cannot be known at compilation time
--> src/main.rs:8:34
|
8 | for (hash, password_data) in &data {
| ^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[_]`
= note: only the last element of a tuple may have a dynamically sized type
It seems the issue is that my hashmap is dynamically allocated and its size is not known at runtime. Is there any way to iterate through the HashMap even though it is dynamically allocated? How would something like this normally be done in rust?
Solution
I'm pretty sure your problem is simply that data
is already a reference, so when you try to iterate over &data
you're iterating over a &&HashMap
, which is not iterable. If you remove the ampersand so that it's for ... in data
I think the loop should work because then it is just iterating over a &HashMap
, which is perfectly fine.
Answered By - BallpointBen Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.