I'm trying to write a simple TCP echo server in Rust, and I'm a little confused about how to return a value from a match in Err.
I know what return type should be usize
, and I would like to return zero. In other languages, I would be simple return 0;
, but Rust does not allow me. I also tried usize::Zero()
. I did it for work by doing let s:usize = 0; s
, but it seems awfully stupid, and I believe that there will be a better way to do this.
let buffer_length = match stream.read(&mut buffer) {
Err(e) => {
println!("Error reading from socket stream: {}", e);
},
Ok(buffer_length) => {
stream.write(&buffer).unwrap();
buffer_length
},
};
I know that I could just not return match
anything and consume buffer_length
internally match
with a function call or something like that, but I would prefer not in this case.
What is the most idiomatic way to deal with something like this?
source
share