TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Learn Rust the Dangerous Way

239 pointsby generichumanover 5 years ago

6 comments

derefrover 5 years ago
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:<p>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 <i>decompile</i> it back into C, hopefully giving a result that will compile back to the same LLVM IR.<p>(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.)<p>Then, walk through the decompilation process again, except this time with Rust semantics, to end up with <i>unsafe</i> Rust that compiles to identical LLVM IR as the C code does.<p>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.<p>The idea here is that this tutorial would be a way of “reading history backwards” (ala <a href="https:&#x2F;&#x2F;slatestarcodex.com&#x2F;2013&#x2F;04&#x2F;11&#x2F;read-history-of-philosophy-backwards&#x2F;" rel="nofollow">https:&#x2F;&#x2F;slatestarcodex.com&#x2F;2013&#x2F;04&#x2F;11&#x2F;read-history-of-philos...</a>). 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.<p>† 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.)
评论 #21865260 未加载
评论 #21865236 未加载
评论 #21865197 未加载
评论 #21865140 未加载
ArmandGrilletover 5 years ago
The notes of the tutorial are well included and answer all the questions I ask myself while reading, it&#x27;s making the tutorial much more enjoyable than the ones I usually read. A great read if you&#x27;re interested in Rust but haven&#x27;t played with it yet IMO.
pgtover 5 years ago
Wayback Machine link from 23 Dec 2019: <a href="http:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20191223074202&#x2F;http:&#x2F;&#x2F;cliffle.com&#x2F;p&#x2F;dangerust&#x2F;" rel="nofollow">http:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20191223074202&#x2F;http:&#x2F;&#x2F;cliffle.com...</a>
评论 #21864331 未加载
评论 #21864111 未加载
sedatkover 5 years ago
This is probably the best Rust tutorial that I’ve ever seen. Feels curated for me.
Reraromover 5 years ago
HN death grip
评论 #21865752 未加载
whoevercaresover 5 years ago
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.
评论 #21867593 未加载
评论 #21867503 未加载
评论 #21866897 未加载