- Rust Programming Cookbook
- Claus Matzinger
- 279字
- 2021-06-24 12:27:49
How it works...
Iterators are a great way of providing advanced capabilities to custom data structures. With their simple, unified interface, collection types can be switched out easily as well and programmers don't have to get used to new APIs for every data structure.
By implementing the Iterator trait in Steps 1 and 2, it becomes easy to provide exactly the desired access level to a collection's elements. In the case of this recipe (and similar to Vec<T>), it will consume the list entirely and remove the items one by one, starting at the front.
In Step 3, we implement IntoIterator, a trait that makes this construct available to the for loop and other users who call into_iter(). Not every collection implements this trait to provide multiple different iterators; for example, the second iterator of Vec<T> is reference-based, and only accessible via an iter() function on the type. By the way, a reference is a data type, just like the actual instance, so it's all about the type definition in this case. These definitions are made inside the trait implementation with the type Item declaration (so-called associated types: https://doc.rust-lang.org/rust-by-example/generics/assoc_items/types.html). These types are called associated types, and can be referenced using Self::Item—just like generics, but without the added syntax verbosity.
With these interfaces, you can get access to a large library of functions that only assume a working iterator to be present! Check out Steps 4 and 5 to see the implementation and outcome of using the iterator on a newly created list type.
We've successfully learned how to implement custom iterators. Now, let's move on to the next recipe.
- DBA攻堅(jiān)指南:左手Oracle,右手MySQL
- 解構(gòu)產(chǎn)品經(jīng)理:互聯(lián)網(wǎng)產(chǎn)品策劃入門寶典
- UML+OOPC嵌入式C語言開發(fā)精講
- C語言程序設(shè)計(jì)學(xué)習(xí)指導(dǎo)與習(xí)題解答
- Java EE 8 Application Development
- 青少年學(xué)Python(第1冊(cè))
- SQL Server與JSP動(dòng)態(tài)網(wǎng)站開發(fā)
- 利用Python進(jìn)行數(shù)據(jù)分析
- 人工智能算法(卷1):基礎(chǔ)算法
- Troubleshooting Citrix XenApp?
- Instant Zurb Foundation 4
- Mastering Gephi Network Visualization
- Python大規(guī)模機(jī)器學(xué)習(xí)
- C/C++代碼調(diào)試的藝術(shù)(第2版)
- Spring MVC Cookbook