Ask coding questions

← Back to all posts
Compile-time values as parameters in functions in Rust?
xxpertHacker

Does Rust have something similar to C++ templates, in the way that they could create compile-time functions and accept compile-time values as template arguments?

Take this trival Rust function:

I could do this in C++:

Values aren't allowed as generic traits,

Is there a way to do something like this in Rust?

Voters
ch1ck3n
Comments
hotnewtop
19wintersp

Hi, sorry, I know this is 6 months old, but I've just realised that this should be possible. Example:

This type of generic also works for impl blocks, etc.

I somehow literally just found this whilst looking through the standard library.

19wintersp

For your example, it'd be like this:

Rust will warn you to name it X though.

xxpertHacker

@19wintersp Hi, sorry, I figured that out about 5 months ago. They're called "const generics." Minimum const generics were stabilized not too long ago.
But yes, they were what I wanted...

19wintersp

@xxpertHacker lol dunno why I remembered this thread, I seemed to think it was relatively recent for some reason

xxpertHacker

@19wintersp Eh, time is relative anyways

xxpertHacker

Hey, @Jakman, do you think you could look at this?

Do you know of an idiomatic solution for this?

Jakman

@xxpertHacker not to my knowledge

xxpertHacker

@Jakman Dang, should I really use macros for this then?

Jakman

@xxpertHacker yeah that sounds like the smartest option

xxpertHacker

@Jakman Okay, time to learn how to use Rust macros I guess.

Jakman

@xxpertHacker I wish I could help with that. Ive never had the need to learn macros. I am a virus dev not an application dev. I will help to the best of my ability so ask questions

xxpertHacker

@Jakman Oh it's okay; I didn't really think that I'd end up needing to learn how to Rust macros, but I'll check check the Rust book, and tutorials.

The only problem that I'll have is time: I'll put this on my scedule, and then get around to it in like 4 months, lmao.

Jakman

@xxpertHacker good to hear. Take care man

19wintersp

Is this what macros are for?

xxpertHacker

@19wintersp So, make a macro that makes a lambda that has the values inlined?

Can you should an example?
Would macro lambdas be idiomatic Rust?

19wintersp

@xxpertHacker I have no clue, to be honest; it was more a question for you lol

xxpertHacker

@19wintersp I ask a question, you response with a question, so I response with a question.

Smart.

19wintersp

@xxpertHacker As far as I know, generics are only for types, and I don't think this is a thing in Rust. Your first example seems more like a decorator, and using a macro might be a bit 😕.

xxpertHacker

@19wintersp Well... my function is actually generic, so I'd have a generic macro returning a lambda, yay!

xxpertHacker

@19wintersp Think you could give an example of how you might write this C++, in Rust?

Assuming T is a primitive that implements std::ops::add.

19wintersp

@xxpertHacker Is this a test?

xxpertHacker
19wintersp

@xxpertHacker Does this even compile? Probably not:

xxpertHacker

@19wintersp Well, you used add instead of Add, but otherwise it would.

Wait no, since you can't use T as a trait in impl Fn(T) -> T.

19wintersp

@xxpertHacker How would you do that then? I'm still confused about generics in some ways.

xxpertHacker

@19wintersp Lmfao, you're asking the wrong person, I don't know Rust.

I know the concrete types, how to use the stdlib, and how to perform addition, but not lifetimes, traits, generics, or macros.