5 ms·
Mostly match recurrence. One pain point about Rust is, due to its linear nature, you cannot use `as` patterns, such as: match e with x as (y, z) -> (x[0], x
by discardable_dan 6y ago
Mostly match recurrence. One pain point about Rust is, due to its linear nature, you cannot use `as` patterns, such as:
match e with
x as (y, z) -> (x[0], x[1]) == (y, z)
This is because the matcher itself must take ownership of the expression, and destructuring is an ownership transfer.
- kevincox 6y agoCan you point me to the swift docs for that? I don't understand what you are trying to do. You can bind references to what you are matching with @name. You also don't need to take ownership of a value. You can match on a reference to it with &. However without understanding your example it is hard to say for sure.
- masklinn 6y ago> 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'.
- kevincox 6y ago> 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 https://rust.godbolt.org/z/eT366s pub fn f(v: &Option<String>) { match &v { other => eprintln!("Doesn't move {:?}", other), } }
- masklinn 6y ago> This is because the matcher itself must take ownership of the expression, and destructuring is an ownership transfer. It doesn't, you can bind by references. It's more fiddly but nothing precludes the combination of at-patterns and sub-pattern matchings, though it's not stable yet there's a tracking issue for at-and-sub-patterns: https://github.com/rust-lang/rust/issues/65490 https://github.com/rust-lang/rust/issues/65490 and it works on nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=61430c8464ed6d0b1af03706c2bf4a51 https://play.rust-lang.org/?version=nightly&mode=debug&editi... There's also an issue for patterns combining by-ref and by-move bindings: https://github.com/rust-lang/rust/issues/68354 https://github.com/rust-lang/rust/issues/68354 Also… does Swift even have as-patterns?
- estebank 6y agoI would personally rely on match ergonomics[1] and borrow e instead of using ref in the pattern[2]. [1]: https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md https://github.com/rust-lang/rfcs/blob/master/text/2005-matc... [2]: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f38ca0d5ab46d44943e8acb086bf633e https://play.rust-lang.org/?version=nightly&mode=debug&editi...
- masklinn 6y agoMatch ergonomics is one thing I really dislike due to how much explicitness it trades for terseness.