Live data from Hacker News

Nibbles of Rust – Restructuring Patterns

catmonad.xyz

1–10 of 31 posts

Re: Nibbles of Rust – Restructuring Patterns

#3
I don't understand why go through the complication of giving this a new name (i.e. "Restructuring Pattern") where the second explanation seems more intuitive and correct.

"Restructuring Pattern" sounds like you can put back together a struct from its constituents, but that's done by normal struct instantiation.

Re: Nibbles of Rust – Restructuring Patterns

#5
The author talks about adding structure, and I've been puzzled about what they are talking about until realizing that they apparently mean that a reference to something is "more" than the something hence it "adds structure". That doesn't make sense to me. More structure is if there are more elements in the data (more fields in a struct, or more alternatives in an enum), whereas a reference is just an indirection of access.

Or am I missing something?

Re: Nibbles of Rust – Restructuring Patterns

#6
Nice tip.

I honestly am a bit frustrated with Rusts pattern matching ergonomics, cuz it ends up requiring me to introduce indentation in places where I don’t want it, mainly to futz around with unwrapping structures or the like.

Lots of helper methods exist on standard library enumerations but I really want there to be some nice built in macros to just give me the innards when I need it, and have a control flow block for when I _cant_ pattern match (and thus want to do an early return or blow up). Maybe this already exists!

Too much indentation makes languages unfun (see every lisp struggle with this with name binding)

Re: Nibbles of Rust – Restructuring Patterns

#7
post #6

Nice tip. I honestly am a bit frustrated with Rusts pattern matching ergonomics, cuz it ends up requiring me to introduce indentation in places where I don’t want it, mainly to futz around with unwrapping structures or the like. Lots of helper methods exist on standard library enumerations but I really want there to be some nice built in macros to just give me the innards when I need it, and have a control flow block…

You mean like ‘matches!’?

https://doc.rust-lang.org/std/macro.matches.html

Re: Nibbles of Rust – Restructuring Patterns

#8
post #6

Nice tip. I honestly am a bit frustrated with Rusts pattern matching ergonomics, cuz it ends up requiring me to introduce indentation in places where I don’t want it, mainly to futz around with unwrapping structures or the like. Lots of helper methods exist on standard library enumerations but I really want there to be some nice built in macros to just give me the innards when I need it, and have a control flow block…

Use combinators!

Re: Nibbles of Rust – Restructuring Patterns

#9
post #7
post #6

Nice tip. I honestly am a bit frustrated with Rusts pattern matching ergonomics, cuz it ends up requiring me to introduce indentation in places where I don’t want it, mainly to futz around with unwrapping structures or the like. Lots of helper methods exist on standard library enumerations but I really want there to be some nice built in macros to just give me the innards when I need it, and have a control flow block…

You mean like ‘matches!’? https://doc.rust-lang.org/std/macro.matches.html

What I want is..

let MyEnum(foo, bar, baz) = input but otherwise { do some stuff return some value }

Point being I don’t have to check for MyEnum-ness twice (like with matches! + if let) and the indented code is for the exception (no match) not the norm.

Tho I … guess !matches(MyEnum{ .. }) is not the worst… but it’s annoying to have this pattern all over my code instead of as some encoded concept we all use

Re: Nibbles of Rust – Restructuring Patterns

#10
post #9
post #7

Earlier quoted context omitted.

You mean like ‘matches!’? https://doc.rust-lang.org/std/macro.matches.html

What I want is.. let MyEnum(foo, bar, baz) = input but otherwise { do some stuff return some value } Point being I don’t have to check for MyEnum-ness twice (like with matches! + if let) and the indented code is for the exception (no match) not the norm. Tho I … guess !matches(MyEnum{ .. }) is not the worst… but it’s annoying to have this pattern all over my code instead of as some encoded concept we all use

You were pretty close with your hypothetical syntax! You can write:

  let Some(y) = x else { do_stuff(); return };
I think it was a fairly recent addition to Rust.
Post reply on HN