Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> You can bind references to what you are matching with @name.

Given what they're saying I assume they're trying to match both the entire value and the sub-values e.g.

    xs@(_:xs')
in Haskell. Stable Rust currently lets you use at-patterns and perform assertions on the sub-pattern, but not match there.

> You also don't need to take ownership of a value. You can match on a reference to it with &.

`ref` is what you'd use in a pattern though, `&` would deref' a reference so usually it'd be to Copy a Copy type which is provided behind a ref'.



> match both the entire value and the sub-values

I see. Yes, it appears to be either-or for now.

> `ref` is what you'd use in a pattern though

Not in the pattern, in the "argument". Like this: https://rust.godbolt.org/z/eT366s

  pub fn f(v: &Option<String>) {
      match &v {
          other => eprintln!("Doesn't move {:?}", other),
      }
  }


You can do both. Your code is closer stylistically to what you'd want to use most of the time, but every now and then I encounter a case where using ref in a single pattern makes more sense.


For sure sure. My point is that you don't have to move out.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: