Live data from Hacker News

A half-hour to learn Rust

fasterthanli.me

121–130 of 342 posts

Re: A half-hour to learn Rust

#121
post #82

Earlier quoted context omitted.

> maybe the site's reading time estimator is broken No, it's just made for prose, not tutorials.

Yep, code is a lot of "words" which throws off the estimate. I need to address this, but I don't think completely ignoring code blocks is really the solution there.

Maybe you could estimate the reading time of a code snippet by taking the square root of the number of lines of code? I imagine that the larger the code snippet, the more irrelevant boilerplate it contains, so the square root might be a good way to model this

Re: A half-hour to learn Rust

#122

Earlier quoted context omitted.

I've only had a passing acquaintance with C and C++, so I've never used aliases. I just googled it... why the heck would you copy pointers? That's insane! In my example, a, b, and c were variables, not pointers.

You keep using this word, variables. I don't think it means what you think it means. At least in my book pointers are still variables (as in "a pointer variable"), and a variable is any named value, whether it's a scalar or a pointer or a nth-pointer, or what its storage is. But you mean that in your example there is no way to affect the previous value, right? > I just googled it... why the heck would you copy pointe…

Variables can be varied... you can increment, decrement, do whatever you want with them. They keep track of things.

Pointers are used reference things allocated from the heap, and never anything else, unless you're insane. Pointers get directly handled in linked lists, trees, etc.

If at all possible, pointers should be avoided otherwise.

Values passed to a procedure can be done by value (the default in Pascal), or by reference (VAR parameters).

I don't see why anyone wouldn't copy values by default... it is the only sane way to do things.

Re: A half-hour to learn Rust

#124

Earlier quoted context omitted.

if I say a = 2; b = a; c = a; There's no pointer in there, just 2.

That will work fine, because 2 is 'Copy', so it can be copied trivially (as are structs consisting only of Copy members which are marked as Copy). Roughly speaking anything with a pointer in it isn't Copy, though many objects will implement Clone, which basically just means you need to explicitly call .clone() to make a copy instead of it happening implicitly.

2 is Copy? It's an integer, could be 2.0 if you wanted to make it a float.

Someone needs to explicitly nail down the mission statement of Rust... because to me it seems to be

"Rust will introduce pointers where they don't need to be, and then try to protect you from the results with obsessive rules"

Re: A half-hour to learn Rust

#125

Earlier quoted context omitted.

You keep using this word, variables. I don't think it means what you think it means. At least in my book pointers are still variables (as in "a pointer variable"), and a variable is any named value, whether it's a scalar or a pointer or a nth-pointer, or what its storage is. But you mean that in your example there is no way to affect the previous value, right? > I just googled it... why the heck would you copy pointe…

Variables can be varied... you can increment, decrement, do whatever you want with them. They keep track of things. Pointers are used reference things allocated from the heap, and never anything else, unless you're insane. Pointers get directly handled in linked lists, trees, etc. If at all possible, pointers should be avoided otherwise. Values passed to a procedure can be done by value (the default in Pascal), or by…

> Variables can be varied... you can increment, decrement, do whatever you want with them.

Variables by themselves are not much. They inherit the properties of the type they are bound to. So what you can do with them can be as restrictive or as permissive as the type allows. That type can be a pointer/reference as well.

Re: A half-hour to learn Rust

#126
post #79

Earlier quoted context omitted.

> I don't understand why b=a; c=a; You know how C has a problem with aliases? (multiple variables referencing the same thing)? Which introduce bugs, prevent optimizations, etc? Well, Rust tries to prevent this, and make more explicit (and known to the compiler) when you do this...

I've only had a passing acquaintance with C and C++, so I've never used aliases. I just googled it... why the heck would you copy pointers? That's insane! In my example, a, b, and c were variables, not pointers.

> why the heck would you copy pointers?

Pointer aliasing is not always obvious.

Re: A half-hour to learn Rust

#127

Earlier quoted context omitted.

That will work fine, because 2 is 'Copy', so it can be copied trivially (as are structs consisting only of Copy members which are marked as Copy). Roughly speaking anything with a pointer in it isn't Copy, though many objects will implement Clone, which basically just means you need to explicitly call .clone() to make a copy instead of it happening implicitly.

2 is Copy? It's an integer, could be 2.0 if you wanted to make it a float. Someone needs to explicitly nail down the mission statement of Rust... because to me it seems to be "Rust will introduce pointers where they don't need to be, and then try to protect you from the results with obsessive rules"

I wrote a piece that tries to explain Rust's memory management strategy, compared to C: https://fasterthanli.me/articles/declarative-memory-manageme...

Hope it helps!

Re: A half-hour to learn Rust

#128
I spent a few hours typing this entire article into an editor just to build muscle memory and I'm amazed. It's exactly the sort of article I needed to learn Rust. I've since thanked Amos on Twitter and I've forwarded links to his blog to a lot of people. This is a great article and does a good job clarifying things to a newcomer.

Re: A half-hour to learn Rust

#129

A half-hour to learn Rust Jan 27, 2020 · 51 minute read · rust maybe the site's reading time estimator is broken? sarcasm intended. But seriously, it is good to have people writing things like this.

I worked my way through the entire article, typing in each code example. Took me about 3 hours.

Re: A half-hour to learn Rust

#130

Earlier quoted context omitted.

That will work fine, because 2 is 'Copy', so it can be copied trivially (as are structs consisting only of Copy members which are marked as Copy). Roughly speaking anything with a pointer in it isn't Copy, though many objects will implement Clone, which basically just means you need to explicitly call .clone() to make a copy instead of it happening implicitly.

2 is Copy? It's an integer, could be 2.0 if you wanted to make it a float. Someone needs to explicitly nail down the mission statement of Rust... because to me it seems to be "Rust will introduce pointers where they don't need to be, and then try to protect you from the results with obsessive rules"

You need to seriously read more on Rust before making judgments. You're clearly confused.

What '2 is Copy' means precisely that Rust WILL NOT 'introduce pointers where they don't need to be' - 2 is fine to 'alias' because it's a primitive type and so a copy is made when you do that, (i.e. the type implements the 'Copy' trait). However when you do that with complex types that do not implement Copy, you're moving ownership to the new variable so cannot use the old one any more.

Post reply on HN