Rust: Iterators

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024

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

  • @williamdroz6890
    @williamdroz6890 Год назад +32

    Iterators are also cool because you can use them with Rayon and get parallelism for free.

    • @FandangoJepZ
      @FandangoJepZ Год назад +4

      But keep in mind that this will in many, especially simpler cases, be incredibly slow

    • @hailuong9295
      @hailuong9295 Год назад +3

      @@FandangoJepZ i mean Parallelism always mean for doing bigger thing, where the cost for work outweight the cost for syncing context, alway choose right tool for right job

  • @BogdanSerban
    @BogdanSerban Месяц назад

    I like your tutorials, straight to the point yet easy to follow 👍

  • @pecet
    @pecet Год назад

    Just wanted to say that I'm enjoying your videos so far. When I'm tired after work writing Swift and Kotlin tests I watch Rust to regenerate.

  • @MeesumQazalbash
    @MeesumQazalbash 7 месяцев назад

    What is your vs code theme? It is kind of cool. Also what extension do you use for the line on the left that makes a bar over a function and its body?

  • @MickAlpine
    @MickAlpine Год назад +1

    Excellent work, loving your Rust series! Quick question - what's your VSCode theme - it looks awesome!

  • @jpgiodevs
    @jpgiodevs Год назад

    Incredibly helpful videos! I hope this year bring's you the notoriety you deserve!

  • @meyou118
    @meyou118 3 месяца назад

    ty - it appears the key to the error for collect is "...because all candidates have the same return type" - imho; the compiler should list them and thats why underscore can be used, the compiler already knew the type of the items. - anyway thx, i just learned something about rust

  • @raylopez99
    @raylopez99 Год назад +1

    As a beginner I had a hard time with the first example, not in compiling it, but in understanding it. So I reworked it as below. The key IMO is that the .collect method will collect the values on the RHS into a vector you can use. See below.
    //
    let v1: Vec = vec![1,2,3];
    let X = v1.iter().map(|x|x+1).filter(|x|*x>2); //note dereference *x, to get value

    println!("{:?}", X); // Prints: Filter { iter: Map { iter: Iter([1, 2, 3]) } }
    let Y: Vec = X.collect();
    println!("{:?}", Y); //prints: [3, 4]
    println!("------------Part Two ------------------");
    let v2: Vec = vec![1,2,3];
    let Q = v2.iter().map(|q| {q+1});
    println!("{:?}", Q); // prints: Map { iter: Iter([1, 2, 3]) }
    let Y2: Vec = Q.collect();
    println!("{:?}", Y2); // Prints: [2, 3, 4]
    //

  • @snk-js
    @snk-js Год назад

    you loved this video, content so smoothly teached. congratz, big brain

  • @irlshrek
    @irlshrek Год назад

    Love it! I think videos on building intuition are super valuable

  • @L0wPressure
    @L0wPressure Год назад +1

    Keep it up, awesome content :)

  • @chouaibsam4381
    @chouaibsam4381 Год назад +3

    I still struggle with the deref concept

    • @TheDevMethod
      @TheDevMethod  Год назад

      Let me know if this video helps ruclips.net/video/UKPpGaqk3ik/видео.html

    • @greisboy425
      @greisboy425 Год назад

      let mut _a = 99;
      // x and y are both pointer, that's why x or y will return memory address not the int value.
      let x = &_a as *const i32; //pointer to x
      let y = &mut _a as *mut i32; //pointer to y
      // accessing or modifying pointer value is unsafe.
      unsafe {
      // y = 100; // this code will not change the value but will change the mem. addr instead
      // and since 100 is not a valid memory address, the program will crash.
      *y = 100; // this is the proper way to access the value, using dereference (with *)
      assert_eq!(100, *x); //another deref to x with *.
      // since both x and y have the same pointer (same memory address), so it will hold 100.
      }

  • @AlexanderHyll
    @AlexanderHyll Год назад +4

    Small addition to the original loop. A while loop is not the idiomatic way to loop over the entire structure (like the iterator). You’re setting a variable in an outside scope that you need to track and that gets cleaned later in the program. You should just do:
    for i in 0..dark_side.len() {
    dark_side[i]
    }
    Now the iterator is often a better way to do it anyways with other added benefits. But it is not that template heavy to start with as the needlessly involved while loop.
    Love the vid though, great job!

  • @xenocampanoli815
    @xenocampanoli815 8 месяцев назад

    Thank you.

  • @farzadmf
    @farzadmf Год назад

    Very nice explanation, thank you!

  • @宝强杨
    @宝强杨 Год назад

    Nice video, help me a lot, Thanks.

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

    Love this vid!

  • @kamertonaudiophileplayer847
    @kamertonaudiophileplayer847 Год назад

    I also use a bit iterators. It is funny, people become so used to with inferring, that even do not know types. Thank you.

  • @736939
    @736939 Год назад +1

    15:05 Shit, this is something new😮

  • @greisboy425
    @greisboy425 Год назад

    When you implement Iterator trait for struct Shoe, your struct (Shoes) magically become an iterator and you can call "into_iter() or iter()" method on your struct. Other iterator method such as "sum()" will also available. But if you try to compile it (with a call to "sum()"), rust compiler will be so unhappy (it won't compile) as it dont know how to total our custom type or struct. I have a question: How to override "sum()" function on Iterator trait, so we can return total or sum of one field in a struct, in your example to return the total size of all shoes...?. My current solution (that I use on one of my project) is to implement method (not a trait function but regular method using "impl MyStruct { pub fn total(&self) -> i32 { self.iter().map(|item|{item.size}).sum()}). But it would be nice if we can just use "mystruct.sum()" (using sum() function from Iterator).

    • @TheBlueye13
      @TheBlueye13 6 месяцев назад

      Have you tried implementing std::ops::add and Default for your struct? The only other thing that vomes to mind is fold(0, |x, obj| x + obj.size)

  • @shapedthought
    @shapedthought Год назад +1

    Can’t believe you didn’t say “turbo fish”!

  • @larrybird3729
    @larrybird3729 Год назад

    amazing!!!!

  • @johnwilliams7999
    @johnwilliams7999 Год назад

    nice video!

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

    Functional programming my man

  • @Anastasia-cv4ld
    @Anastasia-cv4ld Год назад

    What font are you using?

  • @axorusmt7042
    @axorusmt7042 Год назад

    is this a reupload?

    • @axorusmt7042
      @axorusmt7042 Год назад

      don't mean to be rude, but i could have sworn i have seen this video before. maybe just deja vu?

  • @i007c
    @i007c 7 месяцев назад

    you look at your keyboard while typing 🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣