functional-programing/iterator #334
Replies: 5 comments
-
催更 |
Beta Was this translation helpful? Give feedback.
0 replies
-
催更 |
Beta Was this translation helpful? Give feedback.
0 replies
-
习题在英文版:https://practice.course.rs/functional-programing/iterator.html |
Beta Was this translation helpful? Give feedback.
0 replies
-
1 fn main() {
let arr = [0; 10];
for i in arr.iter() {
println!("{}", i);
}
} 2 fn main() {
let mut v = Vec::new();
for n in __ {
v.push(n);
}
assert_eq!(v.len(), 100);
} 3 fn main() {
let v1 = vec![1, 2];
let mut v1 = v1.into_iter();
assert_eq!(v1.next(), Some(1));
assert_eq!(v1.next(), Some(2));
assert_eq!(v1.next(), None);
} 4 fn main() {
let arr = vec![0; 10];
for i in arr.iter() {
println!("{}", i);
}
println!("{:?}",arr);
} 5 fn main() {
let mut names = vec!["Bob", "Frank", "Ferris"];
for name in names.iter_mut(){
*name = match name {
&mut "Ferris" => "There is a rustacean among us!",
_ => "Hello",
}
}
println!("names: {:?}", names);
} 6 /* Fill in the blank */
fn main() {
let mut values = vec![1, 2, 3];
let mut values_iter = values.__;
if let Some(v) = values_iter.__{
__
}
assert_eq!(values, vec![0, 2, 3]);
} 7 fn next(&mut self) -> Option<u32> {
let new_next = self.curr + self.next;
self.curr = self.next;
self.next = new_next;
Some(self.curr)
} 8 fn main() {
let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
// The sum method will take the ownership of the iterator and iterates through the items by repeatedly calling next method
let total = v1_iter.sum::<i32>();
assert_eq!(total, 6);
println!("{:?}",v1);
} 9 fn main() {
let names = [("sunface",18), ("sunfei",18)];
let folks: HashMap<_, _> = names.into_iter().collect();
println!("{:?}",folks);
let v1: Vec<i32> = vec![1, 2, 3];
let v2: Vec<_> = v1.into_iter().collect();
assert_eq!(v2, vec![1, 2, 3]);
} 10 fn main() {
let v1: Vec<i32> = vec![1, 2, 3];
let v2: Vec<_> = v1.iter().map(|x| x+1).collect();
assert_eq!(v2, vec![2, 3, 4]);
} 11 fn main() {
let names = ["sunface", "sunfei"];
let ages = [18, 18];
let folks: HashMap<_, _> = names.into_iter().zip(ages.into_iter()).collect();
println!("{:?}",folks); 12 fn shoes_in_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe> {
shoes.into_iter().filter(|x| x.size == shoe_size).collect()
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
done |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
functional-programing/iterator
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/functional-programing/iterator.html
Beta Was this translation helpful? Give feedback.
All reactions