this post was submitted on 07 May 2025
12 points (100.0% liked)
Rust
6896 readers
58 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Got it, this completely made sense after your explanation and a second look. Also before I saw this example I hadn't thought about being able to pass arrays and tuples as generic parameters types. Thanks
Technically, this may sound pedantic. You are not passing neither arrays nor tuples as generic parameter types.
What you are doing is passing an array to a function.
The type of the array is [i32;5]. Every value has a type.
By passing the array to a function, you are allowing the compiler to infer what function you are calling, since that function is generic. Using the type of the parameter you passed to it.
You can only pass values to function parameters. And you can only pass types as generic type parameters.
Well in this case it's a little different, since it looks like you are passing a value (5) to a generic type parameter (LENGTH), but the
const
part ofconst LENGTH
means that it's a value generic for a type, not a type generic for a type, which is the usual thing.EDIT: additionally, the
: usize
part tells you what type exactly the const parameter for the type has to be.Note that you can't have non-const values as type parameters. Since types are defined at compile time.
EDIT 2: since type inference just fills some boilerplate for you. If we do that boilerplate manually it's easier to see what parameters go where.
When you do
Buffer::from([0,1,2,3,4,5])
what you are really doing is:Buffer<i32, 5>::from([0,1,2,3,4,5)]
. In fact, if you put that, the code will compile exactly the same. Now if you put a 6 instead, it won't compile since the type of the buffer and the type of the array you are passing are not the same.