this post was submitted on 07 May 2025
12 points (100.0% liked)
Rust
6896 readers
40 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
You don't need to know at all what optimizations will happen. I said that as an example of a thing that you know in compile time but not in run time.
To tell or not whether a type will be inferred is determined by you. If you tell the compiler the type, it will never be inferred. If you don't tell the compiler the type, it will try to infer it. If it tries to infer the type but it fails, it will throw a compiler error and it won't finish building the binary.
The compiler will only successfully infer a type if it has enough information at compile time to know with certainty what type it is. Of course, the compiler is not perfect, so it is possible in complex situations for it to fail even though it theoretically could.
Examples where inferring will succeed:
Examples where inference will fail
These situations might be obvious, but inference works as a chain, sometimes hundreds of types are inferred in a single function call. So you should know the basics to diagnose these kinds of problems.