2 ms·
Because you're ---unpacking--- destructuring a Vec2. The most common form of this idiom I've seen is: if let Some(x) = foo() { println!("{:?}", x)
by wizzwizz4 7y ago
Because you're ---unpacking--- destructuring a Vec2.
The most common form of this idiom I've seen is:
if let Some(x) = foo() {
println!("{:?}", x);
}
where fn foo() -> Option<_>, i.e. foo returns either Some(_) or None. (if let is used to account for the possibility of None; it's technically different syntax to let, but very similar.)
Here, x isn't a Some (a variant of Option). It is, however, representing a value inside a Some, which we want to get out. Likewise with your example; we want to destructure from a Vec2, so we specify a Vec2 (with identifiers instead of data) on the left hand side and the data on the right hand side, and it takes the data out and binds it to the identifiers.