<예제 코드_1>
fn main() {
let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
for val in v1_iter {
println!("got: {}", val);
}
}
<예제 코드_2>
#![allow(unused)]
fn main() {
#[test]
fn iterator_demonstration() {
let v1 = vec![1, 2, 3];
let mut v1_iter = v1.iter();
assert_eq!(v1_iter.next(), Some(&1));
assert_eq!(v1_iter.next(), Some(&2));
assert_eq!(v1_iter.next(), Some(&3));
assert_eq!(v1_iter.next(), None);
}
}
<예제 코드_3>
#[test]
fn iterator_sum() {
let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
let total: i32 = v1_iter.sum();
assert_eq!(total, 6);
}
<참조 1> https://rinthel.github.io/rust-lang-book-ko/ch13-02-iterators.html
<참조 2>
'Rust' 카테고리의 다른 글
[Rust] Ownership(소유권) 기초 예제 : i32 (32일차) (0) | 2023.01.14 |
---|---|
[Rust] Ownership(소유권) 기초 예제 : String (31일차) (0) | 2023.01.13 |
[Rust] Command Line Program 제작 : 환경변수 활용 (29일차) (0) | 2023.01.11 |
[Rust] Command Line Program 제작 (28일차) (0) | 2023.01.10 |
댓글