You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this stream you tried to use pattern matching in function arguments and were really close to figuring out the correct syntax.
So not to leave you with the impression that it can't be done, here's how you'd use pattern matching to destructure a tuple and print its individual values:
You use basically exactly the same syntax as if you were to destructure the tuple using a let statement, e.g. let (x, y) = tup; except that the type annotation is mandatory.
Destructuring of more complex values like structs or enums is also possible as shown in the following example:
structCoords{x:i32,y:i32,z:i32,}fnmain(){let coords = Coords{x:2,y:4,z:8};print_coords(coords);}fnprint_coords(Coords{ x, y, z }:Coords){println!("x: {} | y: {} | z: {}", x, y, z);}
Like with let you can only destructure irrefutable patterns, i.e. patterns that will always match the provided values.
The text was updated successfully, but these errors were encountered:
In this stream you tried to use pattern matching in function arguments and were really close to figuring out the correct syntax.
So not to leave you with the impression that it can't be done, here's how you'd use pattern matching to destructure a tuple and print its individual values:
You use basically exactly the same syntax as if you were to destructure the tuple using a
let
statement, e.g.let (x, y) = tup;
except that the type annotation is mandatory.Destructuring of more complex values like structs or enums is also possible as shown in the following example:
Like with
let
you can only destructure irrefutable patterns, i.e. patterns that will always match the provided values.The text was updated successfully, but these errors were encountered: