this post was submitted on 21 Jun 2024
4 points (83.3% liked)
Learning Rust and Lemmy
445 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 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
For me, the biggest things to take away from these chapters were:
Enums and pattern matching for the win
Option
andResult
types are really just applications ofenums
(along with rust's good type system)The example in the book of the
IP Address
enum
type is quite a nice demonstration I think:We're still learning "The Borrow Checker"
structs
andenums
and what best practices arise out of it all.Match statements
match statements
, the concerns are relatively straight forward (I think). Match arms take ownership of the variables they "use/touch" (I'm still unclear on the details there!) ...EG:
match
is on&opt
, thes
in the patternSome(s)
is also a reference because rust implicitly "pushes down" the reference from the outer enum to the inner field or associated data.Borrowing
self
in methodsProbably the trickiest and most relevant part of the two chapters
self
in methods, like any other variable, can be one of three types in terms of ownership:&self
)&mut self
)What's tricky about this is that a method's signature for
self
has consequences that both reach back to the initial type of the root object (ie, is it mutable or not) and forward to what can be done with the root type afterward.&mut self
can't be used on a variable that isn't initially mutable.self
effectively kills the root object, making it unusable after the method is called!!I'm sure there are a bunch of patterns that emerge out of this (anyone with some wisdom here?) ...
But the simple answer seems to borrow
self
, and if necessary, mutably borrow.Taking ownership of
self
is an interesting way to enforce a certain kind of usage and behaviour though.As the object dies, the natural return of an
owning method
would be a new object, probably of the same type.Which leads into a sort of functional "pipe-line" or "method chaining" style of usage, not unlike the "Faux-O" idea in Cory Bernhardt's talk Boundaries. It's likely not the most performant, but arguably has some desirable qualities.
Derivable Traits
structs
where atrait
can be easily implemented for astruct
"automagically":#[derive(Debug)]
EG:
This particular trait,
Debug
, allows for the printing of a struct's full makeup withprintln!
.All of the "Derivable" traits (from the std lib) are listed in Appendix C of The Book
There aren't that many, but they're useful:
Copy
andClone
enable a struct to be copied without having to worry about ownership (though you have to be careful about the types of the fields, as its theircopy
methods that are ultimately relied on)Hash
for hashing an objectDefault
for defining default valuesOf course, when we cover traits we'll learn how to implement them ourselves for our custom types, but these seem to be fundamental features of the language, and easy enough to use right away.
The classical one is something that looks like the following:
This would prevent you from writing code that uses the user's info / session "object" after they have logged out (and/or before they have logged in). On its own it's naive and a bit encumbering - I expect an enum would make more sense but then you can't restrict via types which user session values can and can't be passed to specific functions. In any case, when you are building a whole system around it it can be very useful to have the compiler enforcing these sorts of things "in the background".
This is basically what Gary Bernhardt is talking about in the talk you linked.
Yep. And then you realise that "move semantics" aren't just a safety net that you have to "fight with" but actually a language feature against which you can develop/deploy desirable patterns.
A minor thing I noted reading your code snippets was that I immediately noticed, like at a gestalt level, the lack of ampersands (
&
) and therefore references and could immediately tell that this was a "faux-O"/pipline style system. Not too bad for a syntax often derided as messy/bad.