Live data from Hacker News

Learning Rust via Advent of Code

forrestthewoods.com

51–60 of 85 posts

Re: Learning Rust via Advent of Code

#51
post #19

If you decide to learn Rust using AoC 2018, I highly recommend comparing your work with that of BurntSushi, who is known to write high quality code. https://github.com/BurntSushi/advent-of-code You develop Rust skills by writing your own and then hone your skills by learning from idiomatic Rust.

He links to BurntSushi in the "Parsing Text is Hard" section!

Re: Learning Rust via Advent of Code

#52

I also thought regex seemed overkill and didn't feel like adding an extra crate to my Cargo.toml. Luckily for the days I attempted I was able to get by on just the split method for str's[0] which was nice and concise. So for your regex example you could also do: // #1 @ 916,616: 21x29 let parts: Vec = l.split(['@', ',', ':', 'x'].as_ref()).collect(); let x = parts[1].trim().parse:: ().expect("x as i32"); let y = part…

This is awesome. How did you figure this out? The documentation on `split` says: "The pattern can be a &str, char, or a closure that determines the split." But in your case it is an array of chars and it splits for each of them. I don't see this documented at all.

Vec or an array for instance - [char; 20] can (automatically) create a "slice" of type &[char]. This is true for all types (not just char).

The operator for this is for eg l[1..10]. It can also happen automatically.

&str is the same type as &[char] - just a renaming.

Still learning Rust myself, apologies if this leads you astray. Just do some reading on slices.

https://doc.rust-lang.org/book/ch04-03-slices.html#string-sl...

https://doc.rust-lang.org/std/slice/

Re: Learning Rust via Advent of Code

#53
post #52

Earlier quoted context omitted.

This is awesome. How did you figure this out? The documentation on `split` says: "The pattern can be a &str, char, or a closure that determines the split." But in your case it is an array of chars and it splits for each of them. I don't see this documented at all.

Vec or an array for instance - [char; 20] can (automatically) create a "slice" of type &[char]. This is true for all types (not just char). The operator for this is for eg l[1..10]. It can also happen automatically. &str is the same type as &[char] - just a renaming. Still learning Rust myself, apologies if this leads you astray. Just do some reading on slices. https://doc.rust-lang.org/book/ch04-03-slices.html#strin…

&str is not the same as &[char]. &str has the same memory representation as &[u8]. char is four bytes, not one.

Re: Learning Rust via Advent of Code

#54

I also thought regex seemed overkill and didn't feel like adding an extra crate to my Cargo.toml. Luckily for the days I attempted I was able to get by on just the split method for str's[0] which was nice and concise. So for your regex example you could also do: // #1 @ 916,616: 21x29 let parts: Vec = l.split(['@', ',', ':', 'x'].as_ref()).collect(); let x = parts[1].trim().parse:: ().expect("x as i32"); let y = part…

This is awesome. How did you figure this out? The documentation on `split` says: "The pattern can be a &str, char, or a closure that determines the split." But in your case it is an array of chars and it splits for each of them. I don't see this documented at all.

Would you be willing to file a docs bug so we can clarify this?

It’s possible that it used to be only those types, but was expanded. We should fix this!

Re: Learning Rust via Advent of Code

#55

Earlier quoted context omitted.

what kind of regrets do you have?

I haven't used Nim for anything else since then. If I had to give a simplified critique, the language in itself is still springing to life and the user base and documentation are small. I currently don't have the time to sink into something that could end up being a gimmick only, while there is some hardcore optimization needed in one of our services that could be approached through a better parallelization using Rus…

Hey! Nim core dev here. I was going to offer my support when I saw your original comment but based on your critique so far there isn't that much that I can do, other than keep doing what I'm doing already (evangelizing Nim as much as I can).

This kind of thing does really worry me though, it's almost like a self-fulfilling prophecy, you aren't going to use Nim because you think there isn't enough people using it :(

Do you have any ideas of how we can make you and people like you change their mind?

Re: Learning Rust via Advent of Code

#56
post #47

> I feel like there should be a helper for simple parse operations. > parse!("#{} @ {},{}: {}x{}", id, x, y, w, h); > This would be a clean inverse of println!. The text_io crate does this exactly. https://crates.io/crates/text_io #[macro_use] extern crate text_io; fn main() { let id: u32; let x: u32; let y: u32; let w: u32; let h: u32; scan!("#{} @ {},{}: {}x{}", id, x, y, w, h); }

Ohhh, now that's super handy.

Re: Learning Rust via Advent of Code

#57
post #47

> I feel like there should be a helper for simple parse operations. > parse!("#{} @ {},{}: {}x{}", id, x, y, w, h); > This would be a clean inverse of println!. The text_io crate does this exactly. https://crates.io/crates/text_io #[macro_use] extern crate text_io; fn main() { let id: u32; let x: u32; let y: u32; let w: u32; let h: u32; scan!("#{} @ {},{}: {}x{}", id, x, y, w, h); }

I wonder if we'll ever see `scan!` in the STD lib...

Re: Learning Rust via Advent of Code

#58

I also thought regex seemed overkill and didn't feel like adding an extra crate to my Cargo.toml. Luckily for the days I attempted I was able to get by on just the split method for str's[0] which was nice and concise. So for your regex example you could also do: // #1 @ 916,616: 21x29 let parts: Vec = l.split(['@', ',', ':', 'x'].as_ref()).collect(); let x = parts[1].trim().parse:: ().expect("x as i32"); let y = part…

This is awesome. How did you figure this out? The documentation on `split` says: "The pattern can be a &str, char, or a closure that determines the split." But in your case it is an array of chars and it splits for each of them. I don't see this documented at all.

The docs are a tad misleading but the important piece is to look at the signature. The `where P: Pattern` portion states that split needs a type that implements the Pattern trait. If you follow the link to the Pattern trait then at the bottom you'll see a section[0] listing types implementing the trait (&[char] being one) and therefore can be passed into split. Hope that helps!

[0] https://doc.rust-lang.org/std/str/pattern/trait.Pattern.html...

Re: Learning Rust via Advent of Code

#59
post #2

What is HN-users view of learning a programming language through this kind of problem-solving? Is it limiting? Should this be used as a complement to a larger toy project?

I lean towards keeping to one kind of challenge at a time. If the problems wouldn't be hard in your favorite language, mostly, then this is a great way to exercise a new one. It's fun and you can learn more by checking other people's solutions afterwards.

Re: Learning Rust via Advent of Code

#60
post #33

Earlier quoted context omitted.

Fellow Gopher here interested in learning Rust. Any recommendations?

The Rust book is really good. https://doc.rust-lang.org/book/

It's _so_ good! It's the best technical writing I've read in memory. It's outstanding.
Post reply on HN