Constant Values ​​in Rust Generics

Does the rust language support constant values ​​in common code similar to C ++? The language review does not seem to advertise it. Parameterization of types with constants in C ++ allows you to create objects with pre-allocated buffers of different sizes depending on the needs of the client (for example, stlsoft :: auto_buffer ).
If not, what are the best practices for implementing similar projects in Rust?

+7
source share
1 answer

No, this is not supported by the safe type. We would need numeric type literals, such as the recently added GHC.

However, you can use Rust macros. Using a macro, you can create β€œpatterns” that are parameterized over arbitrary expressions, including constants that let you do what you want here. Please note that you can find errors and limitations in the macro system if you try this at the moment.

+8
source

All Articles