Not necessarily in one specific scenerio, but in general, how can you determine when code will be evaluated at compile time?
I found the following link, and feel like the answer might be right in front of me, but I don't quite understand the text. Maybe I just need examples..
https://doc.rust-lang.org/reference/const_eval.html
So far, I know that
-
const
variables will be evaluated at compile time - Code that's not called will not be in the binary
But I'm not sure about other scenerios. Technically I should learn Assembly... but that seems daunting at this time and I wonder if some more experienced Rust folks have found any hard and fast rules for different kinds of scenerios.
Here's an example
fn main() {
// The builder uses types behind the scenes
// that won't be used anywhere else, they're
// "temporary"
let mut tmp_robot = Builder::new()
.add_arm()
.add_wheels();
init(tmp_robot);
}
fn init(tmp: TmpBot) {
// Build the more efficient runtime
// representation of robot
let bot = Bot::new(tmp);
// This is basically the real start
// of the program and could have
// user inputs and things
simulate(&bot);
}
My assumption with the above example is that everything before let bot = Bot::new(tmp);
will be evaluated at compile time since the program no longer uses anything from before and there was no blocking code yet, like asking for user input or something.
I tried using const fn
but wasn't able to because I use loops and Vecs, and probably some other things. None of it sticks around during the program, but if it ends up in the binary.. That's the question.