News for hackers. Stuff that matters.
Re:Rust 1.43 by djrenren on Thursday April 23, 2020 @01:39PM (#22958570)
This seems like an odd choice... do you know the rationale? (or have a link to an issue/RFC I could go read)
Re:Rust 1.43 by steveklabnik on Thursday April 23, 2020 @02:02PM (#22958876)
I am finding it a bit tough to explain concisely, maybe go check out https://github.com/rust-lang/rust/pull/68129/ and it might help?
Re:Rust 1.43 by majewsky on Thursday April 23, 2020 @04:15PM (#22960697)
It's becoming a recurring pattern in newer languages that number literals are their own magical type (usually backed by a bigint or bigfloat) that only coerce into a concrete type when they first come into contact, so to say, with a typed variable or expression.
Re:Rust 1.43 by kibwen on Thursday April 23, 2020 @04:58PM (#22961320)
Though note that the implementation in Rust is much less magical than that of, say, Go. Every numeric literal expression has a concrete type. When that type can be inferred from context (using the ordinary type inference rules), it is. When it can't be inferred from context, the compiler forces integer literals to i32 and float literals to f64; if that causes additional type errors to occur, then Rust requires the user to insert type annotations where necessary rather than trying to be smart about it (this can all be observed in the error messages in the OP). IOW, code using the numeric literal fallback types only compiles when it makes no semantic difference what the type is (modulo `unsafe`, as ever).

This mostly happens in small code examples and test cases/doctests, e.g. `fn main() { dbg!(42); }` only compiles because of the existence of this fallback.