Ask coding questions

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

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:

fn add_x(x: i32) -> impl Fn(i32) -> i32 {
	move |y| x + y
}

I could do this in C++:

using i32 = int;

template <i32 x>
auto add_x(i32 y) -> i32 {
	return x + y;
}

auto const add_3 = add_x<3>;

Values aren't allowed as generic traits,

fn add_x<x: i32>(y: i32) -> i32 {
	x + y
}

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

Comments
hotnewtop
xxpertHacker (865)

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

Do you know of an idiomatic solution for this?

xxpertHacker (865)

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

Jakman (471)

@xxpertHacker yeah that sounds like the smartest option

xxpertHacker (865)

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

Jakman (471)

@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 (865)

@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 (471)

@xxpertHacker good to hear. Take care man

19wintersp (1121)

Is this what macros are for?

xxpertHacker (865)

@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 (1121)

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

xxpertHacker (865)

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

Smart.

19wintersp (1121)

@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 (865)

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

xxpertHacker (865)

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

template<typename T, T x>
auto add_x(T y) -> T {
    return x + y;
}

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

19wintersp (1121)

@xxpertHacker Does this even compile? Probably not:

use std::ops::Add; // std::ops::add;

fn add_x<T: Add>(x: T) -> impl Fn(T) -> T {
	move |y| x + y
}
xxpertHacker (865)

@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 (1121)

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

xxpertHacker (865)

@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.