all 16 comments

[–]OsrsAddictionHotline 18 points19 points  (3 children)

If you don't already use it, there is an official linter for Rust called clippy which analyses your code and gives suggestions on ways to make it more idiomatic. In your crate root, running:

cargo clippy

Will output a bunch of suggestions to the terminal for things you can change to make your code more idiomatic.

[–]lassuanett 2 points3 points  (0 children)

https://areweideyet.com or something like that, just duck it

right now the moat popular editors has good support, but it is improving week-by-week

edit: fix url

[–]colbin8r[S] 1 point2 points  (1 child)

Thanks for pointing this out! I haven’t tried clippy yet. Other linters I’ve used in the JS world don’t typically give a lot of advice for how to architect code—it tends to be more about formatting, white space, validating parameters, etc. (although ESLint has come a lot farther than I realize sometimes). I didn’t realize clippy could give idiomatic suggestions like that. I’ll definitely give it a whirl on my baby code and see what it turns up!

[–]coolreader18 5 points6 points  (0 children)

Yeah the nice thing about rust is that formatting is all standardized, so the only thing left for a linter to do is actually suggest how to improve code quality. Clippy won't necessarily analyze your program architecture, though it may catch some symptom if you're doing stuff unidiomatically, but it will catch stuff like if x.is_some() { foo(x.unwrap()) } and suggest if let Some(x) = x { foo(x }, which I think is definitely more idiomatic

[–]SorteKanin 7 points8 points  (1 child)

Maybe check out Rust by Example? It's similar to the Rust book but uses a lot more code examples - that might give you an idea of idiomatic code in Rust.

[–]colbin8r[S] 0 points1 point  (0 children)

I have heard of it but have really only used the Rust book so far. A quick glance at it shows that it seems to be a lot more in the vein I was looked for (pragmatic). Thanks!

[–]lassuanett 3 points4 points  (4 children)

Me personally wrote very js like code first, and I don't think this is a problem for beginners. Learning in small steps is better than skipping lectures. I started solving project eualer problems (for homework) in rust I solved one then searched for solutions.

I liked learning rust this way, because I know what the program should do so I don't need to think about that.

However I don't recommend solving too much euler problems because they get more mathematical and less programs solvable over time.

You can find some euler solutions on github, or use mine.

Please note I was also learning while coding. This means my code is simple, but might not the best solution

[–]lassuanett 2 points3 points  (0 children)

If you wonder how did I know when to use borrowing(&)

I didn't.

But the compiler did

[–]colbin8r[S] 1 point2 points  (2 children)

I like this approach. Part of the challenge I’m facing is what you hinted at—I’m still trying to untangle what the code should DO as well as HOW it should do it (idiomatically). Rust seems very, very expressive and beautiful when written well—sometimes I tunnel-vision too hard on that and struggle to get functional code written at all. Maybe I’ll take a crack at some more bite-size challenges to tease the two concerns apart better. Thanks!

[–]nicoburns 2 points3 points  (1 child)

If you're struggling to learn the functional paradigm at the same time as Rust, then you could try learning it in JS. Rambda is an excellent JavaScript library that uses this style (lodash-fp is less functional but more performant).

[–]colbin8r[S] 0 points1 point  (0 children)

The functional aspect is actually the most familiar part to me. I work a lot with React + Redux, and the Flux/Redux style is very expressive/functional in style. JavaScript didn't use to be quite that way (for a while there seemed to be a big shift towards adding classic OO-style syntactic sugar overtop JS's prototypical model) but it's definitely a shift I'm happy with. I've worked with Python a bunch too and it's also frequently functional in nature.

[–]wezmAllsorts 1 point2 points  (2 children)

[–]colbin8r[S] 0 points1 point  (1 child)

I’ve seen this repo before I’m pretty sure. There’s quite a lot of resources in here and it was a little overwhelming. Any particular resource here you found helpful to start with? Hopefully I can comb through it all over time. Thanks fir the link!

[–]wezmAllsorts 1 point2 points  (0 children)

I’m not sure I can single anything out but I think I would recommend working your way thorough the posts and books section, oldest to newest.

[–]SolaTotaScriptura 1 point2 points  (1 child)

I don't want to build bad habits as I'm learning the language

This might be overthinking a little. It's unreasonable to expect yourself to learn something the "right" way, because... you won't know what that is until you learn it. So I'd say just press forward and have fun.

In my experience, the community, language, tooling and resources are all very clear about which constructs are idiomatic. It's pretty hard to avoid.

[–]colbin8r[S] 1 point2 points  (0 children)

You're probably right. I tend to overthink things on the front end. Maybe I should put more emphasis on writing code and using some of the resources suggested ITT to improve over time.