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.
Nibbles of Rust – Restructuring Patterns
11–20 of 31 posts
Re: Nibbles of Rust – Restructuring Patterns
#12Earlier 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.
Re: Nibbles of Rust – Restructuring Patterns
#13The 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…
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
#14I 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.
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
#15The 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…
Re: Nibbles of Rust – Restructuring Patterns
#16Re: Nibbles of Rust – Restructuring Patterns
#17Re: Nibbles of Rust – Restructuring Patterns
#18I think there needs to be a "center the page" browser extension because this is just absurd: https://imgur.com/a/HZqaA4V
document.getElementsByTagName('body')[0].children[0].style.margin = '0 auto';Re: Nibbles of Rust – Restructuring Patterns
#19I 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
#20I think there needs to be a "center the page" browser extension because this is just absurd: https://imgur.com/a/HZqaA4V