How to fix issue on a screenshot? I already tried to make it mutable, but that is not the point. What can it be, how to get rid of it? I will be thankful for the changes in the code.
screenshot:
(source: i.ibb.co )
let mut buf = vec![0 as u8; 4096];
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
match stream.read(&mut buf) {
Ok(size) => {
//Get List of names
let names: LinkedList<String> = serde_json::from_slice(&buf[..size])?;
for name in names.iter() {
if (*data).available.contains_key(&*name) {
//If file already exist just update Vec of IP
(*data)
.available
.get_mut(&*name)
.unwrap()
.push(stream.peer_addr().unwrap());
} else {
//In another case - adding file with first IP that share it
let mut v: Vec<SocketAddr> = Vec::new();
v.push(stream.peer_addr().unwrap());
(*data).available.insert(*name, v);
}
}
}
Err(_) => {
println!("An error occurred, {}", stream.peer_addr().unwrap());
}
}
}
Err(e) => {
println!("Error: {}", e);
}
}
}
Are you sure you want a LinkedList
and not a Vec
as output from your JSON parser? From the LinkedList
docs:
It is almost always better to use
Vec
orVecDeque
because array-based containers are generally faster, more memory efficient, and make better use of CPU cache.
To solve your problem, you should loop over names
instead of names.iter()
. This will make the list unusable after the for
loop. You will then have to remove the dereferences in your code, ie write &name
instead of " &*name
" and name
instead of *name
. However, you shouldn't have written &*name
at all because the &
and *
in &*name
are cancelling each other out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.