January 2019
Beginner to intermediate
554 pages
13h 31m
English
We can also define constant functions that evaluate their argument during compile time. This means that a const value declaration can have a value that's from an invocation of a const function. const functions are pure functions and must be reproducible. This means that they cannot take mutable arguments to any type. They also cannot include operations that are dynamic such as a heap allocation. They can be called in non-const places where they act just like normal functions. But when they are called in const contexts, they are evaluated at compile time. Here's how we define a const function:
// const_fns.rsconst fn salt(a: u32) -> u32 { 0xDEADBEEF ^ a}const CHECKSUM: u32 = salt(23);fn main() { println!("{}", ...Read now
Unlock full access