Rust: Iterators

preview_player
Показать описание
#rust #functional #map

Iterators are one of my favorite parts of Rust. Let's learn about them here as they are discussed in Chapter 13 section 2 of the Rust Programming book

0:00 intro
0:34 Processing a Series of Items with Iterators
5:11 The Iterator trait and the next method
9:27 Methods that consume the iterator
10:17 Methods that produce other Iterators
17:02 Using Closures that Capture their Environment

Links
Chapter 13 section 2 - Processing a series of items with Iterators
Рекомендации по теме
Комментарии
Автор

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

williamdroz
Автор

I still struggle with the deref concept

chouaibsam
Автор

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

meyou
Автор

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?

MeesumQazalbash
Автор

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

BogdanSerban
Автор

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

ic
Автор

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

shapedthought
Автор

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!

AlexanderHyll
Автор

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<i32> = vec![1, 2, 3];

let X = //note dereference *x, to get value

println!("{:?}", X); // Prints: Filter { iter: Map { iter: Iter([1, 2, 3]) } }

let Y: Vec<i32> = X.collect();

println!("{:?}", Y); //prints: [3, 4]

Two

let v2: Vec<i32> = vec![1, 2, 3];

let Q = v2.iter().map(|q| {q+1});
println!("{:?}", Q); // prints: Map { iter: Iter([1, 2, 3]) }

let Y2: Vec<i32> = Q.collect();
println!("{:?}", Y2); // Prints: [2, 3, 4]
//

raylopez
Автор

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

MickAlpine
Автор

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

kamertonaudiophileplayer
Автор

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

snk-js
Автор

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 { But it would be nice if we can just use "mystruct.sum()" (using sum() function from Iterator).

greisboy
Автор

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.

pecet
Автор

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

jpgiodevs
Автор

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

irlshrek