this post was submitted on 07 Feb 2024
21 points (95.7% liked)
Learning Rust and Lemmy
391 readers
1 users here now
Welcome
A collaborative space for people to work together on learning Rust, learning about the Lemmy code base, discussing whatever confusions or difficulties we're having in these endeavours, and solving problems, including, hopefully, some contributions back to the Lemmy code base.
Rules TL;DR: Be nice, constructive, and focus on learning and working together on understanding Rust and Lemmy.
Running Projects
- Rust for Lemmings Reading Club (portal)
- Rust beginners challenges (portal)
- Heroically Helpful Comments
Policies and Purposes
- This is a place to learn and work together.
- Questions and curiosity is welcome and encouraged.
- This isn't a technical support community. Those with technical knowledge and experienced aren't obliged to help, though such is very welcome. This is closer to a library of study groups than stackoverflow. Though, forming a repository of useful information would be a good side effect.
- This isn't an issue tracker for Lemmy (or Rust) or a place for suggestions. Instead, it's where the nature of an issue, what possible solutions might exist and how they could be or were implemented can be discussed, or, where the means by which a particular suggestion could be implemented is discussed.
See also:
Rules
- Lemmy.ml rule 2 applies strongly: "Be respectful, even when disagreeing. Everyone should feel welcome" (see Dessalines's post). This is a constructive space.
- Don't demean, intimidate or do anything that isn't constructive and encouraging to anyone trying to learn or understand. People should feel free to ask questions, be curious, and fill their gaps knowledge and understanding.
- Posts and comments should be (more or less) within scope (on which see Policies and Purposes above).
- See the Lemmy Code of Conduct
- Where applicable, rules should be interpreted in light of the Policies and Purposes.
Relevant links and Related Communities
- Lemmy Organisation on GitHub
- Lemmy Documentation
- General Lemmy Discussion Community
- Lemmy Support Community
- Rust Community on lemmy.ml
- Rust Community on programming.dev
Thumbnail and banner generated by ChatGPT.
founded 9 months ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Even though The Book is a bit verbose in these first few sections and really only touches on the basics of the language, I enjoyed going through it! Once you've gone to the end of just chapter 2, you've touched on project management with
cargo
, compiling withrustc
orcargo
,match
statements, and some of the other syntactical details of the language. It's definitely enough to get you started if you're new to the language!For me, my outstanding questions, which come from the final exercise that builds a "guessing game" (code extracted below for easy reference):
Ordering
actually is and what mechanically happened when it was used in the match statement.rand::Rng
but then writerand.thread_rng().gen_range()
. That I'm importing something not explicitly used in the code (Rng
is never used) feels off to me. I can only guess that this was a shortcut to get to use a higher level interface. But what isRng
?&secret_number
and&secret_number
. Given rust's concern with ownership and the "borrow checker" as a means of preventing memory safety issues ... why isn't it the default behaviour that a variable is passed by reference? Why do we have to explicitly pass the reference ourselves (with the ampersand syntax&secret_number
)?This is just my attempt at answering (I'm learning too):
Macros are easy to use, allowing beginners to write 'hello world' for example, but hide a bunch of complicated stuff we're unlikely to understand until later. (I vaguely remember from Java that just writing something to the console was a whole bunch statements strung together.)
I found it useful to document what each line was doing in the example, to get my head around the terminology.
std
is a crate,io
is a module in thestd
crate, and that module provides methods likestdin()
std
is a crate,cmp
is a module in thestd
crate, and that module provides enums likeOrdering
rand
is a crate,Rng
is a trait from therand
crate, and that trait provides methods likethread_rng()
Ordering
is a enum - a type with a list of variants with developer-friendly names. In the same way that a 'DaysOfTheWeek' enum would have 'Monday', 'Tuesday' ..., etc,Ordering
has Less, Greater, or Equal.match
statements work with enums, in a 'for this, do that' kind of way.Rng
is a trait, which feature a lot in Rust, but is something I've had difficulty getting my head around. Where I'm at so far is they mostly provide convenience. Therand
create provides a number of structures in itsrngs
module -ThreadRng
,OsRng
,SmallRng
, andStdRng
, depending on what you want to use to generate randomness. Instead of sayingThreadRng
provides thegen_range()
method, andOsRng
provides thegen_range()
method, etc, Rust just says they implement theRng
trait. SinceRng
provides thegen_range()
method, it means that everything that implements it will provide it.thread_rng()
returns aThreadRng
structure, which providesgen_range()
via theRng
trait, but we need to bring that trait into scope with theuse
keyword in order to be able to call it.For the default behaviour of passing owned vs. borrowed variables, I guess it's useful to explicitly state "I'm giving this variable to another function, because I don't intend to use it anymore", so that if you inadvertently do, the compiler can catch it and error.
Thanks!!
This makes a lot of sense actually. Thanks! It does lead to the awkward situation where you'd have to know that Rng is the underlying trait of the rest of the module and so import it. But like I said, I'm guessing that's because this example is aiming only for easy high-level usage of
rand
. I'd guess there's a lower level way of using therand
crate that would involve more direct imports, perhapsuse rand::ThreadRng
?Makes sense.
Sooo ... is passing by value a thing in rust? Or does just about every method take only reference types as arguments?
I wondered the same, and tried
use rand::rngs::ThreadRng
but the compiler wouldn't accept it, and suggest I use the trait instead. So it looks like the compiler can be helpful in identifying these things (and maybe traits are the first thing that Rust developers look for when reading the docs for a crate).(wrongness edited out and hopefully corrected in new comment)
Yep. Makes a lot of sense. Probably gotta start thinking in terms of traits at some point. I haven’t spun up any LSP yet but hopefully that can help surface these sorts of things.
Still, at the moment, it does seem like a wrinkle in the usability of the language that you import something which implicitly underlies what you actually want to use.
Also, Thanks!