this post was submitted on 07 Feb 2024
21 points (95.7% 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


Policies and Purposes

  1. This is a place to learn and work together.
  2. Questions and curiosity is welcome and encouraged.
  3. 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.
  4. 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

  1. Lemmy.ml rule 2 applies strongly: "Be respectful, even when disagreeing. Everyone should feel welcome" (see Dessalines's post). This is a constructive space.
  2. 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.
  3. Posts and comments should be (more or less) within scope (on which see Policies and Purposes above).
  4. See the Lemmy Code of Conduct
  5. Where applicable, rules should be interpreted in light of the Policies and Purposes.

Relevant links and Related Communities


Thumbnail and banner generated by ChatGPT.

founded 2 years ago
MODERATORS
 

Hi All! Welcome to the Reading Club for Rust's "The Book" ("The Rust Programming Language"). This is week 1 (the beginning!!).

Have a shot at going through "the reading" and post any thoughts, confusions or insights here

"The Reading"

The Twitch Stream

What's Next Week?

  • Chapters 3 and 4
  • Start thinking about challenges or puzzles to try as we go in order to get some applied practice!
    • EG, Advent of Code
    • Maybe some basic/toy web apps such as a "todo"
you are viewing a single comment's thread
view the rest of the comments
[–] maegul@lemmy.ml 5 points 2 years ago* (last edited 2 years ago) (17 children)

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 with rustc or cargo, 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):

  • Why are macros covered so much later in the book (ch 19)? ... not for mere mortals?
  • I don't think I really know at all what an object like Ordering actually is and what mechanically happened when it was used in the match statement.
  • Why did I import/use rand::Rng but then write rand.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 is Rng?
  • This will probably come up when we cover "Ownership" ... but it strikes me now that we were passing variables by reference (eg &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)?
use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {

    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1..=100);
    // println!("Secret number is: {secret_number}");

    loop {

        println!("Please input your guess.");

        let mut guess = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");

        // let guess: u32 = guess.trim().parse().expect("Please type a number!");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("you guessed: {guess}");

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win");
                break;
            }
        }
    }
}

[–] nmtake@lemm.ee 2 points 2 years ago (3 children)

The Enum Ordering provides compile-time safety. For example, if cmp() takes a string or int, the compiler can't catch invalid inputs ("less", "equal", -123, ...) at compile time and crash at runtime.

[–] maegul@lemmy.ml 2 points 2 years ago (2 children)

Hmm, not sure I’m entirely with you.

If the argument to cmp is of an incorrect or incompatible type (where AFAIU the parent object and argument have to be the same type, eg u32), that alone will be surfaced at compile time no?

If so, then Ordering is actually relatively trivial. It’s an enum, with variants for each possible outcome of a comparison on orderable variables (eg numbers).

And the output of cmp is an Ordering type, which is nice for match statements, as, AFAIU, it forces us to address all possible scenarios (each being a variant of the Ordering enum).

But the compile time safety will come from basic type checking on the argument to cmp.

Am I off base here?

[–] nmtake@lemm.ee 2 points 2 years ago (1 children)

Oh I was completely wrong. cmp() takes a number (not Ordering) and returns Ordering. Sorry for bothering you.

[–] maegul@lemmy.ml 2 points 2 years ago

Pretty sure that’s what this is all about! A safe space to work through the ideas and details without worrying about being wrong. I wouldn’t have understood this better if you didn’t “bother” me and now we (and anyone else reading this presumably) are both better off!

load more comments (13 replies)