Why can't you specify the data type within tuples?

let (name, age:u8) = ("Joe", 10);

Why is this not allowed where I can specify the data type for the age variable inside a tuple?

You would need to specify it for the entire tuple, like so:

let (name, age): (&str, u8) = ("Joe", 10);
1 Like

Thanks this makes sense, and why are string literals &str, like why are they a reference?

That's because they are hard-coded into your program, so you're just referencing the data in your program's memory.

Also, do note that you can use the _ type to let rust infer the type, making the above become:

let (name, age): (_, u8) = ("Joe", 10);

Also, you can't specify types in patterns, and (name, age) is a pattern, but you can use @OptimisticPeach's solution above to specify the types.


Because the following RFC hasn't been approved yet:


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.