I am trying to send data from the server to the client as an "OK" line:

let mut writer = BufWriter::new(&client_stream); ... let answer = String::from("OK"); let size_dat = answer.len(); let _ = writer.write(size_dat);// <------------ ошибка тут let _ = writer.write(answer.as_bytes()) writer.flush().unwrap(); // <------------ проталкивание буферизованных данных в поток 

and I get the error that usize is not at all byte / u8:

 error[E0308]: mismatched types --> <anon>:87:42 | 87 | let _ = writer.write(size_dat); | ^^^^^^^^ expected &[u8], found usize | = note: expected type `&[u8]` = note: found type `usize` 

There was an idea to convert usize to byte / u8 but did not find such a function. Can somehow convert them through a vector or through a String,

 let str_size = String::from(format!("{}", size_dat)); let _ = writer.write(str_size.as_bytes()); 

How to transfer the size of the data sent to the client?

All Code: Playground URL Gist URL

    1 answer 1

    In the English segment of the site there is an answer to your question about converting to u8 https://stackoverflow.com/questions/29445026/converting-number-primitives-i32-f64-etc-to-byte-representations

    • It helped, did this: `let answer = String :: from (" OK "); let size_dat = answer.len (); // convert the size into bytes let size: usize = size_dat; let csize: * const usize = & size; let bp: * const u8 = csize as * const _; let bs: & [u8] = unsafe {slice :: from_raw_parts (bp, mem :: size_of :: <usize> ())}; let _ = writer.write (bs); // helmet 8 bytes data size. let _ = writer.write (answer.as_bytes ()); // helmet data writer.flush (). unwrap (); // <------------ added pushing buffered data to the stream ` - Alex Sotnik