Let's Learn Rust: Fundamentals

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • We continue our Rust learning journey by visiting the fundamentals. This chapter goes over variables and mutability, data types, functions, comments, and control flow. This chapter really challenged some of my assumptions about Rust, reinforcing some and disproving others.
    00:30 - Variables and Mutability
    08:57 - Data Types
    17:25 - Functions
    24:55 - Comments
    25:17 - Control Flow
    Follow along with us at: rust-book.cs.b...
    evcxr(irust): github.com/evc...

Комментарии • 6

  • @omg33ky
    @omg33ky 10 месяцев назад

    I don't think an expression from a for loop (or while loop) is possible, since just putting the value at the end does not work and it is not guaranteed to run into a break with an expression. Even if the code would look like this
    let result = while condition {
    break 3;
    }
    println!("{result}");
    it is not guaranteed to return the 3 since the condition might never be true (or the collection in the for loop might be empty). And since rust does not allow anything that might not be initialized before usage this is a compiler error.
    One thing I really love about rust is that it tells you these things when you try to compile these things.
    This is the output when I try to compile the code above:
    error[E0571]: `break` with value from a `while` loop
    --> src/main.rs:13:9
    |
    12 | let result = while condition {
    | --------------- you can't `break` with a value in a `while` loop
    13 | break 3;
    | ^^^^^^^ can only break with a value inside `loop` or breakable block
    |
    help: use `break` on its own without a value inside this `while` loop
    |
    13 | break;
    | ~~~~~
    and it doesn't really tell you why, but it tells you that break with a value are only allowed in loops or breakable blocks (which are an interesting thing if you really want to do something like that, although I rarely see a reason for something like that).

    • @omg33ky
      @omg33ky 10 месяцев назад

      btw here is an example for the breakable block thing that would allow us to do what we tried to do before (if you don't want to read this feel free to skip this comment :) )
      let result = 'my_block: {
      while condition {
      break 'my_block 3;
      }
      // this is the fallback expression in case we never enter the while and break
      5
      };
      println!("{result}");

    • @JakeCallahan
      @JakeCallahan  10 месяцев назад

      Thanks for taking the time to write up this explanation, very helpful!
      I played around more with for loops after this video and it seems like the resulting expression of a for loop was always a unit.
      Unfortunately, my dreams of using for loops in an assignment have been dashed 😉

  • @blanky_nap
    @blanky_nap 5 месяцев назад

    Great tutorial! I must say after getting in touch with Go, TS/JS and now Rust I appreciate more and more the genius of Guido to develop really user friendly language.

    • @JakeCallahan
      @JakeCallahan  5 месяцев назад +1

      Rust is very opinionated on how you should do things, but with that comes safety. However, I will always love the freedom that Python give us!

  • @dragonkat13
    @dragonkat13 10 месяцев назад

    Great video 😊 love the intros!