-
Rust❤️ 벡터 안에 짝수 인 값의 인덱스 값을 결과값으로 받고 싶을 때 iter().position(|&x| x % 2 == 0)closure코딩Coding/★Rust★기초★Syntax 2022. 6. 23. 17:35728x90
fn main() { let vec = vec![1, 9, 3, 3, 13, 2]; // `iter()` for vecs yields `&i32` and `position()` does not take a reference, so // we have to destructure `&i32` to `i32` let index_of_first_even_number = vec.iter().position(|&x| x % 2 == 0); assert_eq!(index_of_first_even_number, Some(5)); // `into_iter()` for vecs yields `i32` and `position()` does not take a reference, so // we do not have to destructure let index_of_first_negative_number = vec.into_iter().position(|x| x < 0); assert_eq!(index_of_first_negative_number, None); }
출처
https://doc.rust-lang.org/rust-by-example/fn/closures/closure_examples/iter_find.html반응형'코딩Coding > ★Rust★기초★Syntax' 카테고리의 다른 글
Rust 3 situations ( Variable bindings, Pointers, Threads (0) 2022.06.28 Rust❤️Getting a pointer to a field of an enum | Reddit (0) 2022.06.27 Translating C++ & Rust❤️ (0) 2022.06.27 Introduction to Rust - Part 15: ❤️Futures | Rhymu's Videos (0) 2022.06.27 Rust ❤️Tutorial 영어로 써 줘서 불만함 (0) 2022.06.22 Rust 기초 Thread 예문이 죽인다 ❤️ (0) 2022.06.21 Rust❤️] i32::MIN과 fold methods활용법 (0) 2022.06.16 Rust❤️take & collect methods 사용법 1분 53초에 나옴 & skip(3분30초 (0) 2022.06.16