Constant fun (const fn) with Rust - Rainer Stropek - Rust Linz June 2022

Discover the power of constant functions in Rust. Learn how to define constants, use them in traits, and even return impl traits from constant functions. Explore real-world examples and unlock the full potential of Rust's const fn feature.

Key takeaways
  • Constants in Rust do not need to be declared on the top level.
  • Constants can be defined as you know them from other programming languages.
  • Consts can be required by a trait.
  • Consts in Rust do not need to have a name.
  • Rust does it at compile time and writes the result of the constantly evaluated methods into the executable.
  • Constant functions are not limited to applying them at compile time.
  • Constant functions can be generic, and they can even receive const generic attributes.
  • We can also use a variable.
  • We can even override the default value.
  • We can also use constant arrays.
  • We can return an impl trait from a constant function.
  • Traits support consts.