Live data from Hacker News

Nibbles of Rust – Restructuring Patterns

catmonad.xyz

11–20 of 31 posts

Re: Nibbles of Rust – Restructuring Patterns

#11
post #10
post #9

Earlier quoted context omitted.

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.

Thank dog for that. I feel like I even read the release notes for that but it was while I hadn’t been touching a specific codebase with loads of early returns. Thanks for pointing this out!

Re: Nibbles of Rust – Restructuring Patterns

#12
post #10
post #9

Earlier quoted context omitted.

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.

let-else was stabilised in 1.65.0, released 2022-11-03.

Re: Nibbles of Rust – Restructuring Patterns

#13
post #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 a…

&T, &mut T, and T are all different types in a sense. Hence ref is transmuting types during restructuring.

Really, they are adding a new view, so perhaps this is like Haskell lenses, where you can make views with a different type that acts on underlying data

Re: Nibbles of Rust – Restructuring Patterns

#14

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.

The author is talking about restructuring syntax in the destructing match.

Rust Has this with "match" but only via a single ref.

You would have to make alternative restructuring syntax to go into the match expression if you want it to work.

Hypothetically:

   fn g(v: Option) -> Option> {

        match v {
            Some(_Some(_Some(x))),
            None(_None)
        }
    }
Any character with an underscore is a "restructuring" syntax here. Rust only does restructuring with references via the ref keyword and it only does it once per var per expression. The syntax I presented here is hypothetical of course.

I would say it's more syntactic sugar as you can do the restructuring on the right side of the match expression. You can make up an entire functional language using this technique and eliminate the => and everything after it.

I largely agree with you that it's not exactly necessary to do this. Arguably it makes things harder to read.

Re: Nibbles of Rust – Restructuring Patterns

#15
post #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 a…

[dead]

Re: Nibbles of Rust – Restructuring Patterns

#18

I think there needs to be a "center the page" browser extension because this is just absurd: https://imgur.com/a/HZqaA4V

I always thought it was weird when people have websites like this, anyways, for those phased away and want to read this article while centered, execute in the console:

  document.getElementsByTagName('body')[0].children[0].style.margin = '0 auto';

Re: Nibbles of Rust – Restructuring Patterns

#19

I think there needs to be a "center the page" browser extension because this is just absurd: https://imgur.com/a/HZqaA4V

I always thought it was weird when people have websites like this, anyways, for those phased away and want to read this article while centered, execute in the console: document.getElementsByTagName('body')[0].children[0].style.margin = '0 auto';

That's a good tip! I also think its just as weird when sites have zero links to their home page. On what planet is "go to the address bar and delete the last part of the url and hit enter to go to the index" good user experience?

Re: Nibbles of Rust – Restructuring Patterns

#20

I think there needs to be a "center the page" browser extension because this is just absurd: https://imgur.com/a/HZqaA4V

Ctrl+Shift+K for the web console, then `document.body.style.display = "flex"; document.body.style.justifyContent = "center";`.
Post reply on HN