5 ms·
Reading the new Guide: http://doc.rust-lang.org/guide.html http://doc.rust-lang.org/guide.html "The first thing we'll learn about are 'variable bindings.' The
by acqq 12y ago
Reading the new Guide:
http://doc.rust-lang.org/guide.html http://doc.rust-lang.org/guide.html
"The first thing we'll learn about are 'variable bindings.' They look like this:
let x = 5i;
"
And so on: "let (x, y) = (1i, 2i); let x = 5i; x = 10i; "
I had to double check if the default (when the i is not written) is something else than int. It is confusing.
- steveklabnik 12y agoCurrently, there is no default. This is probably changing back. See https://github.com/rust-lang/rust/issues/15526 https://github.com/rust-lang/rust/issues/15526 and the associated issues. The TL;DR is this: not having a default type makes you consider which size to choose. This is generally good. But there are two good cases in which having a default helps a lot: examples and tests. Both of these are small, and don't really care about the details. In real code, inference means you can often not get away with annotating numbers, because the type is inferred. But in these two cases, there often isn't something to do the inference. Long ago, there was fallback, and then it was removed, and now we're considering putting it back. This is a great example of Rust's practicality and a scientific/empiric mindset to building the language.
- acqq 12y agoThanks! I support having the most convenient defaults. Not only for integers, but for other stuff too. Is there a default now for 3.33? Is there a default for "string in quotes"? If there is, integers should be the default too. I believe that everybody can invent as much red tape as he wants, the hard thing is making it concise and effective for the everyday use, not for the writers of compiler. Digressing, were there any discussions about using var like in Swift (instead of let mut)? What is the rationale for not making that more minimalistic?
- steveklabnik 12y agoThe thing is, for floats there's a default that makes sense. For strings, there's a default that makes sense. Integers, on the other hand, are harder. You _probably_ want i32, even on 64 bit machines, but then an i32 can't index an array, so for integers used that way, you want a machine-sized integer.
- acqq 12y ago> for floats there's a default that makes sense Really? In C I use both doubles and floats all the time. > i32 can't index an array Why? Is it because Rust would use less than 32 bits for indexes on some platforms? And how should then the indexes for arrays be written? You have really a hard job, when such basic things change?
- steveklabnik 12y agoUnless you need the extra precision, using a float makes more sense than a double. But with integers, it's different. It's not so much that an i32 _can't_ index an array, but that it _might not be able to_. On a 64 bit machine, you could have a very large array that's bigger than an i32 in size.
- acqq 12y ago> Unless you need the extra precision, using a float makes more sense than a double. No. Floats are OK for big storage, but for a lot of uses you want to make all the intermediate calculations in doubles and only when storing the results to convert to floats again. See https://news.ycombinator.com/item?id=8434128 https://news.ycombinator.com/item?id=8434128 (at the moment top of the front page) where people have problem with Intel using 66 bits mantissa internally for the input of 53 bits (double!). You don't want to have partial results as floats unless you're doing graphics and simple calculations where you need much less bits. You'd would never like to calculate a bridge construction with floats, even if the needed parts would at the end be written with only 4 digits.
- steveklabnik 12y agoThat's what I intended as part of 'if you need the precision.' You're absolutely right that even if the beginning and end are floats, you may want to do the intermediate calculations as a double.
- acqq 12y ago
- nickknw 12y ago> Digressing, were there any discussions about using var like in Swift (instead of let mut)? What is the rationale for not making that more minimalistic? I was interested in the answer to this too. I found these which helped me understand: https://github.com/rust-lang/rust/issues/1273 https://github.com/rust-lang/rust/issues/1273 https://github.com/rust-lang/rust/issues/2643 https://github.com/rust-lang/rust/issues/2643
- azth 12y ago> Digressing, were there any discussions about using var like in Swift (instead of let mut)? What is the rationale for not making that more minimalistic? There were (I think it was somewhere on GitHub). One of the things `let mut` facilitates is: fn main() { let (i, mut j) = (10i, 10i); j += 1; println!("{}", (i, j)); } I'm sure there are other applications for it too (such as in pattern matching). It just fits better into Rust.
- kibwen 12y agoRust doesn't have a default for float types, it forces you to be explicit. `let x = 3.33;` without any other way to infer it will cause an error, forcing you to either write `3.33f32` or `3.33f64`.
- thomasahle 12y ago> This is a great example of Rust's practicality and a scientific/empiric mindset to building the language. Rust sometimes seems like the antithesis to BDFL. I quite like that they experiment, but I'm wondering how this will work out post 1.0.
- CUViper 12y agoThe whole point of 1.0 is that they won't experiment like this anymore. They're using the freedom to break things now with the promise to lock it down later. I'm sure there will still be some experimentation, but the core language should be stable.
- jeffdavis 12y agoOne thing rust has going for it is a very clear vision (safety, control, speed), which is based on fundamental concepts (e.g. lifetimes). Consequently, it has less of a need for a BDFL to declare what is or is not rust-ific.
- MichaelGG 12y agoThe default seems to be "inferred". That is, 1 + 1i will work, but 1 + 1 is ambiguous. I've just noticed this in use, and don't know what this is officially called. I don't think this will actually be a real annoyance in practice.
- kibwen 12y agoIt's just type inference. Rust doesn't do automatic integer coercion on arithmetic operations, so once it knows the type of any of the numbers it can deduce the types of all the rest.