-6

I am trying to implement an HTTP client in Rust using this as a starting point. I was sent to this link by the rust-lang.org site via one of their rust-by-example suggestions in their TcpStream page. I'm figuring out how to read from a TcpStream. I'm trying to follow this code:

fn handle_client(mut stream: TcpStream) {
    // read 20 bytes at a time from stream echoing back to stream
    loop {
        let mut read = [0; 1028];
        match stream.read(&mut read) {
            Ok(n) => {
                if n == 0 { 
                    // connection was closed
                    break;
                }
                stream.write(&read[0..n]).unwrap();
            }
            Err(err) => {
                panic!(err);
            }
        }
    }
}

Where does the n variable come from? What exactly is it? The author says it reads 20 bytes at a time; where is this coming from?

I haven't really tried anything yet because I want to understand before I do.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    [Patterns](https://doc.rust-lang.org/1.6.0/book/patterns.html) – Sean Bright Jun 05 '19 at 18:09
  • 3
    I recommend reading the [chapter on `match` in the book](https://doc.rust-lang.org/book/ch06-02-match.html), in particular the subsection "Patterns that Bind to Values". The variable `n` is declared and bound in the pattern `Ok(n)`, similar to `err` being declared in the pattern `Err(err)`. – Sven Marnach Jun 05 '19 at 18:09
  • You need to perform more [research](https://stackoverflow.com/questions/14154753/how-do-i-make-an-http-request-from-rust) – Jeroen Heier Jun 05 '19 at 18:14
  • Based on the code you've provided, there's nothing about 20 bytes. Since there's no link to your original source, we can't tell you if you've transcribed the code incorrectly, there's information outside of the provided code that would explain it, or the author is wrong. – Shepmaster Jun 05 '19 at 18:24

2 Answers2

0

I strongly encourage you to read the documentation for the tools you use. In this case, The match Control Flow Operator from The Rust Programming Language explains what you need to know.

From the Patterns that Bind to Values section:

In the match expression for this code, we add a variable called state to the pattern that matches values of the variant Coin::Quarter. When a Coin::Quarter matches, the state variable will bind to the value of that quarter’s state. Then we can use state in the code for that arm, like so:

fn value_in_cents(coin: Coin) -> u8 {
    match coin {
        Coin::Penny => 1,
        Coin::Nickel => 5,
        Coin::Dime => 10,
        Coin::Quarter(state) => {
            println!("State quarter from {:?}!", state);
            25
        },
    }
}

If we were to call value_in_cents(Coin::Quarter(UsState::Alaska)), coin would be Coin::Quarter(UsState::Alaska). When we compare that value with each of the match arms, none of them match until we reach Coin::Quarter(state). At that point, the binding for state will be the value UsState::Alaska. We can then use that binding in the println! expression, thus getting the inner state value out of the Coin enum variant for Quarter.

There is an entire chapter about the pattern matching syntax available and where it can be used.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
-1

Figured it out, this is what's happening:

match stream.read(&mut read) {

This line is telling the software to pass stream.read(&mut read) to Ok(n) because stream.read returns the number of bytes read. I'm still not sure why they specify 20 bytes at a time as being read.