mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-28 06:59:11 +00:00
update repo layout
This commit is contained in:
21
en/src/functional-programing/iterator.md
Normal file
21
en/src/functional-programing/iterator.md
Normal file
@ -0,0 +1,21 @@
|
||||
# Iterator
|
||||
|
||||
https://doc.rust-lang.org/stable/rust-by-example/flow_control/for.html#for-and-iterators
|
||||
|
||||
```rust,editable
|
||||
// (all the type annotations are superfluous)
|
||||
// A reference to a string allocated in read only memory
|
||||
let pangram: &'static str = "the quick brown fox jumps over the lazy dog";
|
||||
println!("Pangram: {}", pangram);
|
||||
|
||||
// Iterate over words in reverse, no new string is allocated
|
||||
println!("Words in reverse");
|
||||
for word in pangram.split_whitespace().rev() {
|
||||
println!("> {}", word);
|
||||
}
|
||||
|
||||
// Copy chars into a vector, sort and remove duplicates
|
||||
let mut chars: Vec<char> = pangram.chars().collect();
|
||||
chars.sort();
|
||||
chars.dedup();
|
||||
```
|
Reference in New Issue
Block a user