News for hackers. Stuff that matters.
Re:Rust 1.43 by nevi-me on Thursday April 23, 2020 @12:05PM (#22957431)
The primitive type inference is helpful, especially given the number of `val as i32` conversions that one might encounter when doing arithmetic from different crates. It however feels "magical", so I'm not sure of how I feel about it.

Do the primitive type inferences also work when comparing i32 vs &i32, or even better, &i32 vs &u64?

Re:Rust 1.43 by simias on Thursday April 23, 2020 @12:38PM (#22957844)
I believe that this only works for literal type inference, i.e.:

    let a: f32 = 0.0;
    let b: f64 = 0.0;
    
    let this_works: f32 = &0.0 + 0.0;
    let this_doesnt: f32 = &a + b;
And that's a good thing IMO, implicit promotion rules in C are one of my least favourite features of the language. It makes it so easy to write seemingly innocuous code that's completely broken.
Re:Rust 1.43 by ridiculous_fish on Thursday April 23, 2020 @06:37PM (#22962364)
What is &0.0 + 0? Adding a reference to a float to a float - is it being automatically dereferenced?
Re:Rust 1.43 by steveklabnik on Friday April 24, 2020 @10:45AM (#22968485)
Yes, it is being automatically de-referenced.
Re:Rust 1.43 by steveklabnik on Thursday April 23, 2020 @12:42PM (#22957886)
Yes, it is only for literals.
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.

Re:Rust 1.43 by steveklabnik on Thursday April 23, 2020 @12:27PM (#22957719)
Yes, all combos of &T and T on both sides of the operator do. Sorry for not being more explicit about that in the post!