Would compile time code execution be theoretically possible with Rust?

I was watching this video of Jonathan Blow explaining how the language he's developing allows him to compile the bare minimum necessary of his program to execute whatever function is marked by "#run", and have the compiler run it, get the results and put that right into his source code and then keep compiling everything else

Would that be theoretically be possible with Rust?

If that is possible, I think it would be extremely useful for people like me who are new to Rust in general. I would be able to get "macro-like" behaviour using just regular Rust.

1 Like

Well, there's always the possibility to write arbitrary compile-time behavior as procedural macro. You can even have your main.rs have only one line: macro_crate::macro!(), and then generate the whole source code inside the macro.

There's also quite powerful const evaluation now.

2 Likes

I wouldn't say quite powerful.
The RFC has been stripped down to the most bare version in order to get it out the door.

Pretty much the only thing it'll let you do in the beginning is abstract over array-like types, and thus it has the potential to make arrays more ergonomic to use. Technically you can also use other primitive types rather than just numbers, but until arbitrary user defined types are allowed I don't see much point in that.

Here is the tracking issue for const evaluation features: https://github.com/rust-lang/rust/issues/57563

I believe you're confusing const eval with const generics.

Mm, yeah. 1.46 will bring const loop/while, and if/match, and allow for things like const-sha1. (I think that's only a liitle over 2 weeks away from stable release.)

There's also build scripts (Build Scripts - The Cargo Book) which are arbitrary Rust code that runs at compile time.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.