My question is why I got this error. And how can I fix it?
error[E0515]: cannot return value referencing local variable `buf`
--> src/main.rs:16:5
|
8 | let map: HashMap<&str, &str> = str::from_utf8(&buf)
| ---- `buf` is borrowed here
...
16 | map
| ^^^ returns a value referencing data owned by the current function
Left side of : is a pattern, which performs certain actions. In your case &buf means copying the input to a local, owned buf variable (& on the left of : is like * on the right).
If you use buf: &[u8; 1024], it should be correctly borrowing the data instead of copying.