2 ms·
Thank you! What is "<"? Full line is: let mut chunks = ArrayVec::<&[u8; CHUNK_LEN], NUM_INPUTS>::new(); Edit: I probably should not comment to everyone one
by johnisgood 5y ago
Thank you! What is "<"? Full line is:
let mut chunks = ArrayVec::<&[u8; CHUNK_LEN], NUM_INPUTS>::new();
Edit:
I probably should not comment to everyone one-by-one, so thank you all for the answers!
- chipsa 5y agoThat looks like a template instantiation to me. It's "<&[u8; CHUNK_LEN], NUM_INPUTS>" So the angle brackets are wrapping two arguments to the template.
- Tehnix 5y agoIn Rust, <> are used for type arguments. E.g. if you make a function with a generic type, it would be `fn my_func<T>(arg1: T) { ... }` Now, what we have here is commonly referred to as the Turbo Fish[0] operator (the `::<>` syntax). In this case it's there to help let the compiler know which concrete type you want to create your new `ArrayVec` with :) [0]: https://doc.rust-lang.org/1.30.0/book/first-edition/generics.html#resolving-ambiguities https://doc.rust-lang.org/1.30.0/book/first-edition/generics...
- tlamponi 5y agoThose are used for generic in that case. Foo<T> supports more types, often limited with certain traits they must support. Here it's passing the actual type on instantiation, in that case there are two types: an u8 slice and a number of inputs, just guestimating the latter as I did not check the original definition in the source. See https://doc.rust-lang.org/book/ch10-01-syntax.html https://doc.rust-lang.org/book/ch10-01-syntax.html for a better explanation.
- cornstalks 5y ago<> is used in Rust similar to how C++ uses them in templates. ArrayVec::new() is a generic function that takes two generic arguments: a type (&[u8; CHUNK_LEN]) and a value (NUM_INPUTS). So ArrayVec is the structure, <...> are the generic arguments (I replaced stuff with ellipses for brevity), and new() is the function call. :: is used as a "path" separator to syntactically differentiate between all these different parts.
- stusmall 5y agoIt is the opening of a generic type parameter. In this case ArrayVec[1] takes two, a type T for the element type and a const for the maximum size of the ArrayVec. The use of ::<> is affectionately known as "the turbofish" and is a way to give the compiler type information when it can't infer it. 1. https://docs.rs/arrayvec/0.7.1/arrayvec/struct.ArrayVec.html https://docs.rs/arrayvec/0.7.1/arrayvec/struct.ArrayVec.html
- SAI_Peregrinus 5y agoGeneric type. Just like C++ or Java, really. So `ArrayVec::<T, const CAPACITY: usize>` is an ArrayVec of items of type T, with capacity CAPACITY (of type usize, think size_t). An ArrayVec is a vector backed by a fixed-size array. Basically an array with convenience methods on it.[1] In this case, T is `&[u8; CHUNK_LEN]` and `CAPACITY` is `NUM_INPUTS`. `&[u8; CHUNK_LEN]` is roughly equivalent to a `uint8_t* array[CHUNK_LEN]`. So to simplify even more it's a fixed-size vector of byte arrays. The details of why it's not quite equivalent to that C code (how Rust checks the safety) are beyond the scope of this comment. [1] https://docs.rs/arrayvec/0.7.1/arrayvec/struct.ArrayVec.html https://docs.rs/arrayvec/0.7.1/arrayvec/struct.ArrayVec.html
- aortega 5y agoRust syntax really is crazy. It's designed to be utterly incomprehensible.