Live data from Hacker News

Rust: “Explain GATs Like I'm 5 Years Old”

old.reddit.com

81–90 of 193 posts

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#81

The top Reddit example is tremendous. I'm passed being a Rust beginner, but I have a note to the Rust community: Your official examples are overcomplicated and bad and you should also feel a little bad. Stick to things like apple, orange, pear and you'll see much easier adoption than if you go with something like LendingIterator! Through the GAT process all I have seen is the same hyper-specific example used that con…

This is exactly why open source communities need more than programmers.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#82

I'm filing this one along with Monads and Haskell as programming concepts that I'll never understand. Rust is my favorite language, but that explanation, lauded directly below it as This is personally the easiest to understand example I've seen of GATs. , was incomprehensible.

Wait. Why are Monads complicated? Yes the math definition is complicated, but so is math definition of number 1 in set theory

You have a Rust-like enum that contains data like Option or Maybe, you want to abstract over it. There you have a rudimentary Monad.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#84

Is there any way to use GATs to achieve a particular use case of ah-hoc/anonymous enums[0]? Say if library L1 returns enum A|B, L2 operates on enum A|B, and main wants to pass enum A|B from L1 to L2, with the kicker being A, B are concrete in main. I'm not interested in distinguishing between multiple appearances of the same type as in the RFC example, so rather like: let foo: (~str|int) = (_|666); match foo { (s: st…

These are called sub-types or pattern types, and they are not in the language. But they are something I would love to have. You can’t emulate them with GAT unfortunately. https://cohost.org/oli-obk/post/165584-ranged-integers-via

I've not heard this called sub- or pattern types before. Perhaps the rfc syntax is complicating the discussion.

We have struct and tuple in Rust where tuple is an ad-hoc/anonymous struct where only the number and order of member types matter. We have enum which are nominal types. We don't have the ad-hoc/anonymous version of them where only the set of possible types matter.

In TypeScript, F#/OCaml they would be like x: TypeA|TypeB

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#85

The top Reddit example is tremendous. I'm passed being a Rust beginner, but I have a note to the Rust community: Your official examples are overcomplicated and bad and you should also feel a little bad. Stick to things like apple, orange, pear and you'll see much easier adoption than if you go with something like LendingIterator! Through the GAT process all I have seen is the same hyper-specific example used that con…

As a Rust beginner, have to completely agree... A lot of the examples are overtly complex. Beyond this, sometimes finding appropriate use case examples for libraries harder still. Mostly in that you have to have a very deep knowledge base to even begin in a lot of ways. I mean, I get it, but it's still much harder to get your feet wet beyond "Hello World" type examples in practice.

Note: been a rust beginner for a while, as I play with it, (re)read a couple books I have on Rust, but then set it aside again as I'm unable to justify using it vs. just getting something done. I really like a lot of the concepts but digging in has been repeatedly difficult.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#86

I feel like they are making languages too complicated. Most projects should just be garbage collected, most don't need to deal with pointers or anything advanced. We need variables, functions, conditionals, loops, exception handling, struts, and a library with a bunch of standard utilities (including simple I/O and string managment). Everything else should be optional and kept out of sight unless you are looking for…

`&` and `'` do mean something: `&` indicates something that's borrowed, and `'` indicates a lifetime. (`static` is a specifically blessed identifier given to a certain lifetime.) This is explained in https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#... , and the static lifetime is explained in https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#... .

It's also possible to have `&str`s with smaller lifetimes than `'static`, in which case you will have a different lifetime specifier there.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#87
post #82

I'm filing this one along with Monads and Haskell as programming concepts that I'll never understand. Rust is my favorite language, but that explanation, lauded directly below it as This is personally the easiest to understand example I've seen of GATs. , was incomprehensible.

Wait. Why are Monads complicated? Yes the math definition is complicated, but so is math definition of number 1 in set theory You have a Rust-like enum that contains data like Option or Maybe, you want to abstract over it. There you have a rudimentary Monad.

> you want to abstract over it.

What does that mean? Any example you could point to?

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#88

The top Reddit example is tremendous. I'm passed being a Rust beginner, but I have a note to the Rust community: Your official examples are overcomplicated and bad and you should also feel a little bad. Stick to things like apple, orange, pear and you'll see much easier adoption than if you go with something like LendingIterator! Through the GAT process all I have seen is the same hyper-specific example used that con…

>Stick to things like apple, orange, pear

While I am no pedagogical psychologist, I have witnessed mutiple times, how poor unrealistic examples give people a bad understanding of the thing they are trying to learn. Going with Apple, Orange, Pear would give people an idea of what the keywords did, but no understanding of when it was a good idea to apply the technique.

I think that is one of the primary reasons OO sucks so badly today. Everyone was trained on Fruit, Shapes, and Cars so they know the keywords, but don't have the foggiest clue of when it is a good idea to use them.

Not that I am suggesting LendingIterator! is a super wonderful example either. A good motivating example like 95% of the difficulty, and reward of programming tutorials.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#89
post #52

Earlier quoted context omitted.

I think the easiest way to understand it is this: Suppose you have a concrete type like Vec Regular generics enable you to make the contained type generic, so you have: Vec where T can be i32, u32, String, etc. GATs allow you to make the container generic. So you can have: T where T might be Vec, Option, Box, etc.

Thanks, this was much easier to understand than the original article. What will I use this for? Usually for the “T” to be useful it needs to have some interface. In Java I’d just use Collection or something more abstract.

T keeps its type info statically. So if you put a Set in, you can get a Set out.

Casting to Collection loses the static type info, so you couldn't write foo(bar(set)) if bar returned a Collection but foo accepted a Set.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#90
post #60

Earlier quoted context omitted.

It is easy to understand because it is an oversimplification :)

Which part is inaccurate/incomplete?

The part that says `T`. GATs don't allow you to do that, not directly. GATs allow you, as said by Boats in https://news.ycombinator.com/item?id=33506540, have a generic type as an associated type, nothing less, nothing more. If you want to express `T`, GATs _enables_ you to do that as a kind of a distorted encoding, but it's not as simple as https://news.ycombinator.com/item?id=33505810 makes it seem.
Post reply on HN