Popular Rust Iterator Methods 🦀

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ม.ค. 2025
  • Rust iterators are incredibly powerful, allowing you to manipulate collections of various data structures. Because the Iterator Trait defines this functionality, these useful methods can be found on any custom types that implement it. While we don't have enough time to cover all of these methods, we will explore some of the more common methods, and spend some hands-on time with them.
    For example, we can take a second iterable collection, and .chain() it to the first one. We can also .zip() collections together into tuple types. You can perform filters, data transformations, and much more, using the Rust iterator methods. I strongly encourage you to spend some time using these methods, to ensure that you understand how they work, when you actually need them.
    🤯 Rust Programming Playlist 🦀 • Rust Programming Tutor...
    📖 Rust Iterator docs ➡️ doc.rust-lang....
    Visual Studio Code ➡️ code.visualstu...
    Rust Website ➡️ rust-lang.org
    Rustup Installer ➡️ rustup.rs
    Rust Docs ➡️ doc.rust-lang....
    Please follow me on these other social channels!
    ➡️ trevorsullivan...
    ➡️ github.com/pcg...
    ➡️ / pcgeek86
    ➡️ / trevorsullivan
    ➡️ / trevorsoftware
    ➡️ tiktok.com/pcg...
    All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names,trademarks and brands does not imply endorsement.
    #rustlang #rust #rustdev #opensource #software #linux #devops #programming #rusty #dev #coding #codinglife #code #coder #ubuntu #ubuntulinux #appdev #developer
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 29

  • @TrevorSullivan
    @TrevorSullivan  ปีที่แล้ว +3

    Check out the full Rust playlist for more Rust programming videos! 🦀 ➡➡➡➡ th-cam.com/play/PLDbRgZ0OOEpUkWDGqp91ODn0dk7LPBAUL.html

  • @irlshrek
    @irlshrek 11 หลายเดือนก่อน +18

    You can simplify transforming a Vec into a Vec by using vec_of_str.map(String::from) instead of using a full closure. This works because .map requires that the function you use takes the same type and number of arguments as the items in the vector. In this case, String::from fits perfectly as it takes a single &str argument and is not a method of &str, but rather a function that operates on &str to produce a String.

    • @FourLowAdventures
      @FourLowAdventures 7 หลายเดือนก่อน +2

      I'm currently doing Rustlings and spent all morning trying to convert a Vec into a Vec. Then I randomly see this comment. Thanks!

    • @irlshrek
      @irlshrek 7 หลายเดือนก่อน

      @@FourLowAdventures woo! You got this

    • @vgsuresh
      @vgsuresh 11 วันที่ผ่านมา

      @@irlshrek thanks for sharing the tip, but for this worked `first_names.into_iter().map(String::from);` I had to call `into_iter()` before using map.

  • @thedarkknight-3894
    @thedarkknight-3894 ปีที่แล้ว +11

    This is the exact type of content i was looking for learning rust. Many educators just copy the content from the rust book and severely lack originality and explanation.

    • @TrevorSullivan
      @TrevorSullivan  ปีที่แล้ว +6

      Thank you so much!! That makes me so happy to hear. I intentionally avoid using examples straight out of the documentation, to add variety to the topic! ❤️

  • @trevorbeingtrevor
    @trevorbeingtrevor ปีที่แล้ว +8

    Chapter 13.2 in the rust book seems to assume the reader has more experience with iterators than I did when reading it. This video helps fill in some of those gaps, thanks!

  • @johndavies729
    @johndavies729 3 หลายเดือนก่อน +1

    Thanks for the video. I learnt a few new useful methods here. Can you please make the text wrap around so it doesn’t go off the screen?

  • @stevedsouza4307
    @stevedsouza4307 10 หลายเดือนก่อน +3

    Thank you for these videos. They are really helpful. The quality of content is far better than the paid subscription I use.
    If I can suggest, it would be nice to see a detailed video on smart pointers, their necessity, usage etc. Thanks.

  • @ChidleyITCompute
    @ChidleyITCompute 10 หลายเดือนก่อน +2

    Very helpful video, like the others in this series.
    isn't &str a borrowed string slice and not a static string? A static string has a lifetime equal to the whole program, whereas the lifetime of &str may not be.

    • @TrevorSullivan
      @TrevorSullivan  10 หลายเดือนก่อน +2

      Good question. I was under the impression that it has an inferred 'static lifetime, but maybe there's other ways to use it.

    • @dubstepaztec3573
      @dubstepaztec3573 6 หลายเดือนก่อน

      @@TrevorSullivan It has whatever lifetime you give it. If you don't give it an explicit lifetime like `static or `a then it lives until the current scope ends. I think it can be kinda confusing cause we tend to think of &str as it's own special thing but it's pretty much just a pointer to some bytes with a length. I have no idea what str (&str without the ref) is under the hood tbh but &str functions as any other reference type

    • @qweqwewerwerertert
      @qweqwewerwerertert 3 หลายเดือนก่อน +1

      A String is a heap allocated struct similar to vector. A str is a primitive and is on the stack, and not the heap. You can create pointers to both. A string literal (straight up “xyz” in your code) is a slice of the binary itself (that is, a pointer+length to somewhere in your binary).

  • @meowsqueak
    @meowsqueak ปีที่แล้ว +3

    Thanks for this.
    I think it’s useful for beginners to know that you can use the variable name in format strings - e.g. println!(“{index} {value:?}”);
    This can save quite a lot of typing when starting out. Unfortunately it’s not quite as powerful as Python’s f-strings, as you can’t put any expression in the {} like {value.0}. I feel like this is an unfortunate omission since Rust is so expression-based. Oh well :)
    I think sum(), product() and count() are more useful and intuitive to beginners than fold(), but it’s good to mention it for more complex items like those quantity tuples. I’m just slightly surprised you went there so quickly :)

    • @TrevorSullivan
      @TrevorSullivan  ปีที่แล้ว +2

      Thanks for the tip on var names in format strings! Much appreciated. I do actually like how Python and JavaScript template strings work, so you can embed method calls or other expressions in them.

  • @brianobot2543
    @brianobot2543 21 วันที่ผ่านมา

    When I do become something with Rust, I might not have the avenue to tell you directly that your contents made a greater portion of the rustness i would have in me

  • @RichardWalterLanda
    @RichardWalterLanda ปีที่แล้ว +3

    pls, add time codes.

  • @bujibujibuji-z4z
    @bujibujibuji-z4z 3 หลายเดือนก่อน

    Hi Trevor. I have a question.
    for fruit: &&str in fruit_iter{
    println!("{}", fruit);
    }
    why the type of fruit is &&str instead of &str?
    Thank you!

    • @TrevorSullivan
      @TrevorSullivan  3 หลายเดือนก่อน

      @@bujibujibuji-z4z hello! That's a really good question. The &str is a borrowed string, so I'm assuming it's some sort of "reference to a reference". Unfortunately I don't know the right terminology, or enough about memory layout, to try to explain this. I would recommend posting that question to the official Rust forum! I have seen this before and would love to understand it better.

    • @bujibujibuji-z4z
      @bujibujibuji-z4z 3 หลายเดือนก่อน

      @@TrevorSullivan Thank you. 🙏

  • @alokswaincontact
    @alokswaincontact 5 หลายเดือนก่อน

    Is the source code that you worked on in this video available somewhere?

    • @TrevorSullivan
      @TrevorSullivan  5 หลายเดือนก่อน

      @@alokswaincontact no I haven't published it anywhere. I usually recommend writing it yourself, as it's good practice.

  • @ImranKhan-br5dv
    @ImranKhan-br5dv 9 หลายเดือนก่อน

    never learnt this much from any other video

  • @m0j0x00
    @m0j0x00 9 หลายเดือนก่อน

    my rust programs are over 2Gb can you make a video on how to minimize the size of binaries ??

    • @TrevorSullivan
      @TrevorSullivan  9 หลายเดือนก่อน +2

      Wow! Those are massive binaries! I would recommend using "cargo build --release"

    • @m0j0x00
      @m0j0x00 9 หลายเดือนก่อน

      @@TrevorSullivan I want to make binaries with 3 - 5KB for offensive security.

  • @agnesakne4409
    @agnesakne4409 26 วันที่ผ่านมา

    Where can I earn 200k p.a. with rust?