Live data from Hacker News

A half-hour to learn Rust

fasterthanli.me

31–40 of 150 posts

Re: A half-hour to learn Rust

#31
post #19

Earlier quoted context omitted.

I just made a similar comment before I read yours, but about C# rather than C, where just like C you can do: `int x = 42;` But since int (32-bit integer) is the default integer type, you can also do: `var x = 42;` If you want to use another type, for example, ulong (unsigned, 64-bit integer), you can do: `ulong x = 42;` Or: `var x = 42ul;` C#'s syntax is not only terser, but seems a lot easier to read to me. With rus…

It's only clear because you already know what "long" and "int" mean in C#. It's potentially quite confusing for someone coming from a C or C++ background as they mean different things there. i32 and u32 on the other hand are ambiguously 32 bits. In C++ I've actually had code using a value like `42l` that works on Linux but not on Windows, because the sizes of those types aren't fixed.

> In C++ I've actually had code using a value like `42l` that works on Linux but not on Windows, because the sizes of those types aren't fixed.

if you want fixed size for literals you can use the "function macros for integer constants" :

    #include 
    auto x = INT32_C(-456456435); // guaranteed at least 32-bit
    auto y = UINT64_C(45645654654321685768); // guaranteed at least 64-bit.

Those will enclose the constant in the proper literal suffix - ull on 32-bit windows and ul on linux for instance.

Re: A half-hour to learn Rust

#32
post #12

> let x: i32 = 42; I'm sorry but this notation will always make me scream. C is so much simple: int x = 42; not "let", no colon, and no ambiguous "i32 = 42"

How about `let x = 42` and let type inference do the work? I used to do a lot of C-style variable declarations in other languages but I'm warmed up to Rust's really fast because most of the time I don't need to explicitly name the type.

"let" should be removed, and it doesn't change that the type should be placed before, not after with a colon.

Re: A half-hour to learn Rust

#34
Nice. What is dont understand is why is the second Vec2 used in this example? It introduces x and y, which are of type float right, not Vec2?

let v = Vec2 { x: 3.0, y: 6.0 }; let Vec2 { x, y } = v; // `x` is now 3.0, `y` is now `6.0`

Re: A half-hour to learn Rust

#35
post #24

Earlier quoted context omitted.

It's only clear because you already know what "long" and "int" mean in C#. It's potentially quite confusing for someone coming from a C or C++ background as they mean different things there. i32 and u32 on the other hand are ambiguously 32 bits. In C++ I've actually had code using a value like `42l` that works on Linux but not on Windows, because the sizes of those types aren't fixed.

A valid point about knowing what the types mean, but even if you don't, it is at least immediately apparent which part is the type , and which part is the value . I've only dabbled with rust, but I came across this very early on, and was baffled by the syntax. After further dabbling, I still can't see it and immediately know what the value is.

AFAIK, syntax is:

let variable_name [: type] = value[(i|u|f)bits];

In rust, 123l is written 123i64 or 123i32 (also 123_i32, or 1_2_3i32), depending on what 123l actually means.

I don't actually use rust, but it seems very clear and obvious to me (obvious once you know the syntax above).

Re: A half-hour to learn Rust

#36
post #11

Underscore is not exactly "throw away" but rather it is exactly "don't bind to variable". The difference becomes evident with the statement: let _ = x; This is a no-op and the value remains owned by the variable x, and is not thrown away.

I debated which terminology to use and thought "throwing away" was more intuitive, especially if you've never heard of "binding" before. It has its limits though - in your example I'd say you're throwing away the result of evaluating "x", just as if you did: x;

I don't agree, because we can try the two following programs, and one of them does not compile. It compiles with underscore.

1:

    let x = String::new();
    let _ = x;
    println!("{}", x);
2:

    let x = String::new();
    x;
    println!("{}", x);   // ERROR: Use of moved value x.

And this is why I said that let _ = x; is a no-op. :)

Re: A half-hour to learn Rust

#37
post #34

Nice. What is dont understand is why is the second Vec2 used in this example? It introduces x and y, which are of type float right, not Vec2? let v = Vec2 { x: 3.0, y: 6.0 }; let Vec2 { x, y } = v; // `x` is now 3.0, `y` is now `6.0`

Because you're ---unpacking--- destructuring a Vec2.

The most common form of this idiom I've seen is:

    if let Some(x) = foo() {
        println!("{:?}", x);
    }
where fn foo() -> Option, i.e. foo returns either Some(_) or None. (if let is used to account for the possibility of None; it's technically different syntax to let, but very similar.)

Here, x isn't a Some (a variant of Option). It is, however, representing a value inside a Some, which we want to get out. Likewise with your example; we want to destructure from a Vec2, so we specify a Vec2 (with identifiers instead of data) on the left hand side and the data on the right hand side, and it takes the data out and binds it to the identifiers.

Re: A half-hour to learn Rust

#38

What a nice approach to presenting the language in a clear cut way. We should have this for every language out there.

ive been perusing a bit of rust code for the past 2 years, reading an article here and there....always heard it was a complex and low level , next generation C language. comparing it to go etc... but never wanted to learn it... This article is _so_ simple in its delivery. It really got excited in the 'beauty' presented by each simple chunk.

Re: A half-hour to learn Rust

#39
post #35
post #24

Earlier quoted context omitted.

A valid point about knowing what the types mean, but even if you don't, it is at least immediately apparent which part is the type , and which part is the value . I've only dabbled with rust, but I came across this very early on, and was baffled by the syntax. After further dabbling, I still can't see it and immediately know what the value is.

AFAIK, syntax is: let variable_name [: type] = value[(i|u|f)bits]; In rust, 123l is written 123i64 or 123i32 (also 123_i32, or 1_2_3i32), depending on what 123l actually means. I don't actually use rust, but it seems very clear and obvious to me (obvious once you know the syntax above).

Meh, 123_i32 seems like a big improvement to me, but with the others, I just can't immediately grok it - it's having numeric digits as part of the type name that throws me.

I realise of course that not everyone will feel the same.

Re: A half-hour to learn Rust

#40
post #12

> let x: i32 = 42; I'm sorry but this notation will always make me scream. C is so much simple: int x = 42; not "let", no colon, and no ambiguous "i32 = 42"

The C approach makes the compiler much more complex, and introduces extra typing in other language constructs. (like parens around if statements) This is why many newer languages do something more like the Rust way. Overall it is simpler for programmer and compiler.
Post reply on HN