Rust conversion int, string, array, u8

This is my server.
I want my server to get the value of variable B, change the value of A, and SAVE this chandes, and send to the client the new value of A in array[u8,50]
My question is how can I convert from string to utf8, but array[u8,50]?

fn handle_client(mut stream: TcpStream) {
    let mut data = [0 as u8; 50]; // using 50 byte buffer
    let mut a: i32 = 1;
    while match data) {
        Ok(size) => {
           let temps = str::from_utf8(&data).unwrap();  //get the utf8 and made String         
           let temp: i32 = temps.parse().unwrap(); // convert from String to i32
         // actually, I don't know, how to do in the way, when
         // when my variable A doesn't change to the beginning value - 1,
         // every time, when the client sends new value of B ??
            a = a * temp;
            let st = a.to_string(); // now, converting a:i32 to String
            data = st.into_bytes(); // now I need to convert String to u8, but the error says:
             // expected array [u8,50]
             // found strust 'std::vec::Vec<u8>'
             // so I need to convert String to array [u8,50], BUT HOW?
            // echo everything!
        Err(_) => {
            println!("An error occurred, terminating connection with {}", stream.peer_addr().unwrap());
    } {}

You can copy the string data to the array with


Note that you should inspect the value of size, since only the first size bytes are changed by read.

Or you can write it into tcp stream without allocation.

write!(stream, "{}", a).unwrap();

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.