Live data from Hacker News

Nibbles of Rust – Restructuring Patterns

catmonad.xyz

21–30 of 31 posts

Re: Nibbles of Rust – Restructuring Patterns

#22
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…

I think it's “more” in the same sense that Some(x) is more than x. You can destructure &x in exactly the same way.

Re: Nibbles of Rust – Restructuring Patterns

#23

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

Text becomes hard to read when lines are much over 70 characters. I don't know that there's a universally preferred solution. Personally, I only use movie-shaped windows for movies, and page-shaped windows for everything else.

Re: Nibbles of Rust – Restructuring Patterns

#24

Since the author mentions at the end that they were motivated to write and publish this as a part of a writing group, I'd like to say that this was a very pleasant read. Tightly-scoped but insightful and informative!

Thanks! (Post author. I made an HN account to reply here, haha.)

Re: Nibbles of Rust – Restructuring Patterns

#25

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';

I'll go ahead and promise to leave the '#wrapper' id on that element in my blog template, so this can be shortened to:

  document.getElementById('wrapper').style.margin = '0 auto';
Though if you're in the habit of customizing sites you read, I'd personally recommend using a browser extension like Stylus (https://add0n.com/stylus.html) to do CSS, so you could write it like this:

  #wrapper {
    margin: 0 auto;
  }

Re: Nibbles of Rust – Restructuring Patterns

#26

Earlier quoted context omitted.

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?

Agreed. It was a good tip.

My goal in leaving out the link to my home page was to make the Writing Gaggle home page more prominent than my personal blog. (I manage hosting both, see.) I only added my blog index page as an afterthought when I saw a ton of 404 errors on my server a few months ago because people were manually navigating to it.

I'll consider adding actual navigation to my page template though, so maybe you'll be less annoyed ;)

Re: Nibbles of Rust – Restructuring Patterns

#27
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…

The set of all &T is larger than the set of T.

Let’s consider the case where T is u8. There are 256 inhabitants of the u8 type (0u8-255u8).

Now consider &u8. Each u8 referent is still some value between 0 and 255, but the number of references you could have is dependent on the pointer width of your target system — let’s say 64 bits. So that’s 2^8 possible u8 values times 2^64 possible references, or 2^72 inhabitants. Well, minus one to account for null, and I’m probably missing other details, but you get the idea.

So going from T to &T introduces structure in the same sense that going from T to (T, U) does; and going from &T to T destructures in the same sense that going from (T, U) to T does. That structure may not be directly observable (as in the case of a reference: you’d have to go out of your way to observe the underlying address), but conceptually the structure is there nonetheless.

Edit:

Another way to look at it is this:

Consider a Rust “newtype”, like struct Days(u8).

If you pattern match on that to obtain the inner u8, we call that destructuring. Destructuring is to reduce or break down the structure of a thing, so the opposite must be building up structure, or “restructuring”. Therefore going in the opposite direction — stuffing a u8 value in a Days - must be restructuring. In this case both types have the same number of inhabitants (256); regardless, one direction is destructuring and the other direction is restructuring.

Re: Nibbles of Rust – Restructuring Patterns

#28
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.

What determines whether `let Some(y) = x` evaluates to boolean true?

Re: Nibbles of Rust – Restructuring Patterns

#29
post #28
post #10

Earlier quoted context omitted.

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.

What determines whether `let Some(y) = x` evaluates to boolean true?

if x is `Some`, then `y` is set to the inner value and the following block occurs. you can also use this with other enums - i don't remember off the top of my head if it works for all enums, but you can at least also use it with `Result`

Re: Nibbles of Rust – Restructuring Patterns

#30
post #28
post #10

Earlier quoted context omitted.

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.

What determines whether `let Some(y) = x` evaluates to boolean true?

The fact that the pattern matches.

Here the type being matched is:

    enum Option {
        None,
        Some(T)
    }

    let x = Some(42);
    let Some(y) = x else { panic!("None") };
Post reply on HN