What does "Err(_) | something()" do?

I found this syntax in a lib, and I never saw it before...

loop {
        let mut buf = vec![0; 1024];
        let n = match stdin.read(&mut buf).await {
            Err(_) | Ok(0) => break,
            Ok(n) => n,

In patterns, _ matches without binding. So that matches the error variant without binding the value within. Effectively the value is ignored.

If you've seen

let _ = call_that_returns_something_you_want_to_ignore();

It's the same mechanism, only deeper within a pattern.

After reading your other post, I think I misunderstood and you're asking about the pipe. That's an or-pattern. In the example it matches any Err and Ok(0), but not Ok(1) etc.

1 Like

I meant the "|" sign ^^

1 Like

thank you.

1 Like

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.