I'm trying to write a simple TCP echo server in Rust, and I'm a little confused over how to return a value from a match to Err.
I know the return type is supposed to be usize, and I'd like to return a zero. In other languages, I would just return 0; but Rust doesn't let me. I've also tried usize::Zero(). I did get it to work by doing let s:usize = 0; s but that seems awfully silly and I imagine there would be a better way to do it.
let buffer_length = match stream.read(&mut buffer) {
Err(e) => {
println!("Error reading from socket stream: {}", e);
// what do i return here?
// right now i just panic!
// return 0;
},
Ok(buffer_length) => {
stream.write(&buffer).unwrap();
buffer_length
},
};
I know I could also just not have the match return anything and consume buffer_length inside the match with a function call or something, but I'd prefer not to in this case.
What is the most idiomatic way to handle something like this?