News for hackers. Stuff that matters.
Re:Learn Rust the Dangerous Way by derefr on Monday December 23, 2019 @11:28AM (#21864702)
I’ve had an idea for a while now, of writing a tutorial that works similar to this one, but backwards. I have no time to write the tutorial, but here’s the idea:

Behind the scenes, write a short C program, and compile it into LLVM IR through Clang. Present the code first as its LLVM IR representation, then work through how you’d manually decompile it back into C, hopefully giving a result that will compile back to the same LLVM IR.

(You’d be teaching some of the fundamentals of compiler theory here, in “reverse order”, but IMHO that’s exactly what you need if you’re to understand where C and Rust really diverge.)

Then, walk through the decompilation process again, except this time with Rust semantics, to end up with unsafe Rust that compiles to identical LLVM IR as the C code does.

And then, finally, treating the unsafe Rust as the new IR, walk through how you’d decompile that unsafe Rust into safe Rust, introducing each static-analysis compiler pass that Rust does in reverse, adding one feature at a time, until the code is all safe Rust, but can be theoretically partial-compiled into the earlier unsafe-Rust code†, and from there into the same LLVM IR as the other two examples.

The idea here is that this tutorial would be a way of “reading history backwards” (ala https://slatestarcodex.com/2013/04/11/read-history-of-philos...). I.e., rather than coming into a world with a fully-formed Rust and a fully-formed C, you’d start with a world that only has ASM; and then introduce C into it; and then introduce Rust into it, seeing how each introduction changes what you can do.

† It’d be helpful if rustc had a mode where it could emit some sort of “core Rust” IR, that was all unsafe{} and had only the barest types, in the vein of early C++ compilers that emit C, or how the Erlang compiler works. Sadly, I don’t think rustc is architected in a way that’d allow this. (Though, paging steveklabnik to correct me.)

Re:Learn Rust the Dangerous Way by roblabla on Monday December 23, 2019 @12:32PM (#21865260)
> It’d be helpful if rustc had a mode where it could emit some sort of “core Rust” IR, that was all unsafe{} and had only the barest types, in the vein of early C++ compilers that emit C, or how the Erlang compiler works. Sadly, I don’t think rustc is architected in a way that’d allow this. (Though, paging steveklabnik to correct me.)

MIR seems to be the closest thing to a "Core Rust". You still have access to all the types, but everything is desugared down to the barest possible syntax. While and for loops are desuggared to loop+break, complex field accesses are split into temporary to ease borrow analysis, etc...

I'm not sure what happens to unsafe block, but I suspect those gets stripped as well, and MIR is assumed to be valid. There's a tool called miri that can be used to find UB by running the MIR - if you manage to trigger UB, miri will error out telling you what went wrong.

Might be worth looking into it :D

Re:Learn Rust the Dangerous Way by masklinn on Monday December 23, 2019 @12:29PM (#21865236)
> It’d be helpful if rustc had a mode where it could emit some sort of “core Rust” IR, that was all unsafe{} and had only the barest types

Rust has a "core Rust" called MIR. It has nothing to do with unsafe and definitely doesn't remove types though. It's mostly about desugaring constructs, so by and large a simplified version of Rust (though it has things normal Rust doesn't, namely goto).

Alternatively, mrustc compiles Rust via C.

Re:Learn Rust the Dangerous Way by gdxhyrd on Monday December 23, 2019 @12:25PM (#21865197)
Compiling is a destructive process. While you can definitely learn a lot by reading IR and figuring a program out, and while it is easy to show why programming in ASM is time consuming, I don't think it makes sense to teach compiler theory and compiler passes that way.

In general, moving from one language to a "higher level" or "stricter one" (such as unsafe Rust to safe Rust) requires a redesign to properly do it (if at all possible).

Re:Learn Rust the Dangerous Way by xtagon on Monday December 23, 2019 @12:19PM (#21865140)
Love the idea! If/when you end up writing this, I'd love to be notified (or if you have a blog I'll just subscribe now) :-)
Re:Learn Rust the Dangerous Way by ArmandGrillet on Monday December 23, 2019 @04:32PM (#21867609)
The notes of the tutorial are well included and answer all the questions I ask myself while reading, it's making the tutorial much more enjoyable than the ones I usually read. A great read if you're interested in Rust but haven't played with it yet IMO.
Re:Learn Rust the Dangerous Way by pgt on Monday December 23, 2019 @09:56AM (#21863935)
Re:Learn Rust the Dangerous Way by iudqnolq on Monday December 23, 2019 @10:48AM (#21864331)
Does anyone have copies of the next articles in the series? Wayback doesn't.
Re:Learn Rust the Dangerous Way by masklinn on Monday December 23, 2019 @11:43AM (#21864845)
Do you mean the hypothetical part 6? It doesn't exist. Part 5 was published on December 14th but the author hasn't released anything since (I wouldn't be surprised if they were busy given it's the end-of-year).
Re:Learn Rust the Dangerous Way by iudqnolq on Monday December 23, 2019 @11:45AM (#21864864)
No, I mean parts 2-5
Re:Learn Rust the Dangerous Way by masklinn on Monday December 23, 2019 @11:55AM (#21864939)
All of them work for me right now.
Re:Learn Rust the Dangerous Way by mijoharas on Monday December 23, 2019 @01:45PM (#21865991)
Really? I get a similar error for part 2, a link from the way back machine links here: http://web.archive.org/web/20191223074202/http://cliffle.com...

How were you able to see it?

Re:Learn Rust the Dangerous Way by iudqnolq on Monday December 23, 2019 @11:59AM (#21864964)
On Wayback or directly? It times out for me directly and isn't indexed by Wayback afaict
Re:Learn Rust the Dangerous Way by masklinn on Monday December 23, 2019 @12:26PM (#21865206)
Direct. Tried again, works fine. It probably got a bit under the weather when it finally reached the front page, especially given it's also at the top of /r/programming right now.
Re:Learn Rust the Dangerous Way by adriantam on Monday December 23, 2019 @10:21AM (#21864111)
Thanks. But more deep down, I wonder why and how some links nowadays will be dead on slashdot effect or not.
Re:Learn Rust the Dangerous Way by sedatk on Tuesday December 24, 2019 @03:05AM (#21870635)
This is probably the best Rust tutorial that I’ve ever seen. Feels curated for me.
Re:Learn Rust the Dangerous Way by Rerarom on Monday December 23, 2019 @09:49AM (#21863894)
HN death grip
Re:Learn Rust the Dangerous Way by brobinson on Monday December 23, 2019 @01:23PM (#21865752)
Death Grips is Online
Re:Learn Rust the Dangerous Way by whoevercares on Monday December 23, 2019 @02:34PM (#21866528)
Nowadays it seems quite rare to find someone using C for low level without solid CS background. In fact I bet a good percent of CS major undergraduate or even graduate can’t write good C code.
Re:Learn Rust the Dangerous Way by atoav on Monday December 23, 2019 @04:29PM (#21867593)
My girlfriend who doesn't really programm worked with C on a Arduino. I don't have any CS backgound at all and use it constantly on embedded systems (be it Arduino, Teensy, or some MCU).
Re:Learn Rust the Dangerous Way by jswizzy on Monday December 23, 2019 @04:20PM (#21867503)
Electrical engineers write plenty of C and assembly where I work.
Re:Learn Rust the Dangerous Way by jeffdavis on Monday December 23, 2019 @03:11PM (#21866897)
I'm confused about how the two sentences are related. Can you elaborate?
Re:Learn Rust the Dangerous Way by whoevercares on Monday December 23, 2019 @04:38PM (#21867655)
It might make more sense during the ages when programming is still considered a speciality, then CS as a major has some virtue and it’s largely a pre-req. Of course nowadays the bar is low enough