'Type annotation needed when use byteorder with vector
I need to build a packet to send to the server. I use byteorder crate for this. One of my field contains usize data, so I use typecasting to convert it to the i16:
use byteorder::WriteBytesExt;
fn main () {
let login = "test";
let packet_length = 30 + (login.len() as i16);
let mut packet = Vec::new();
packet.write_u8(0x00);
packet.write_i16(packet_length);
packet.append(&mut Vec::from(String::from("game name ").as_bytes_mut()));
// ... rest code
}
But I got an error:
packet.write_i16(packet_length);
| ^^^^^^^^^ cannot infer type for type parameter `T` declared on the associated function `write_i16`
This is playground to reproduce. https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1e5dd29816a128f7561e06f7c825864d
I am confused where I should to add type annotation here, could you please help?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
