while let
expressions
if
์ ๋์ผํ๊ฒ while let
๊ตฌ๋ฌธ ์ญ์ ํจํด๋งค์นญ์ ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
Like with
if
, there is awhile let
variant which repeatedly tests a value against a pattern:
fn main() { let v = vec![10, 20, 30]; let mut iter = v.into_iter(); while let Some(x) = iter.next() { println!("x: {x}"); } }
v.iter()
๊ฐ ๋ฐํํ ๋ฐ๋ณต์๋ next()
๊ฐ ํธ์ถ๋ ๋๋ง๋ค Option<i32>
๋ฅผ ๋ฐํํฉ๋๋ค.
์ด๋ None
์ด ํธ์ถ๋๊ธฐ ์ ๊น์ง Some(x)
๋ฅผ ๋ฐํํ๋ค๋ ์๋ฏธ๋ก ๊ฒฐ๊ณผ์ ์ผ๋ก while let
์ ๋ฐ๋ณต์์ ๋ชจ๋ ์์ดํ
์ ๋ฐ๋ณตํ๋๋ก ํ๊ฒ ํด์ค๋๋ค.
๋ฌ์คํธ์ ํจํด๋งค์นญ์ ์ฐธ์กฐํ์ธ์
Here the iterator returned by
v.iter()
will return aOption<i32>
on every call tonext()
. It returnsSome(x)
until it is done, after which it will returnNone
. Thewhile let
lets us keep iterating through all items.See pattern matching for more details on patterns in Rust.