30

When doing something like:

let mut sum = 5 + 10;

What is the exact type of sum? Is it an arbitrary-size type which can’t be overflowed?

4

1 Answer 1

38

There is RFC 212, which states:

Integer literals whose type is unconstrained will default to i32

If you want there is a clippy lint default_numeric_fallback that can warn you about unwanted fallback.

6
  • Where was it validated?
    – Boiethios
    Apr 29, 2019 at 12:27
  • 2
    @FrenchBoiethios There is a link to the issue but I believe that the RFC repository only merge a RFC when it's accepted so by definition if a RFC is on the master branch, it has been accepted and still up to date. "Eventually, somebody on the core team will either accept the RFC by merging the pull request, at which point the RFC is 'active', or reject it by closing the pull request." github.com/rust-lang/rfcs/blob/master/text/0002-rfc-process.md
    – Stargateur
    Apr 29, 2019 at 12:30
  • @Stargateur is it also the same for integers returned by functions ? Apr 29, 2019 at 12:34
  • 4
    @user2284570 functions require to be explicit, so you can't write such functions. You only way would be to use generic, and let the compiler infer the type of the generic with a variable you didn't type, for exemple: play.integer32.com/…
    – Stargateur
    Apr 29, 2019 at 12:42
  • Did they change the default? My IDE is telling me my numbers are i64. Maybe it's inferring that type from somewhere, idk. Jul 27, 2020 at 17:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.