24 ms·
Rusty ownership and the lifecycle’s stone
- kapolos 4y agoA tongue-in-cheek exploration of how Rust achieves memory safety & performance, for people interested in Rust coming from high-level (managed) languages.
- 29athrowaway 4y agoThis article is simply impossible to read.
- bornfreddy 4y agoIgnoring (or hiding) the images, the explanation is actually the best one I have seen so far and well worth the read. I guess some people like the fun images too. /shrug
- IOT_Apprentice 4y agoI found that not to be the case, and that the explanations were clear. I thought the images were contextually amusing. What specifically was problematic for you to read it?
- meindnoch 4y agoI never thought I would feel like I’m watching TikTok when reading an article from Harvard, but here we go…
- Mxrtxn 4y agoBest explanation I have ever heard! Really good examples. Hope to see more.
- nayuki 4y ago"Mob program" made me giggle. It must be the logical extension to pair programming.
- asplake 4y agohttps://en.wikipedia.org/wiki/Mob_programming https://en.wikipedia.org/wiki/Mob_programming
- taberiand 4y agoThere's a lot of talk in general how Rust has a steep learning curve, mainly due to the borrow checker and lifetimes - but is this as hard as it gets? Everything here seems logical and fairly self-explanatory, the only slightly alien thing is the lifetime annotation. Are there advanced gotchas that can actually trip up an experienced coder? Are there some aspects of real work that Rust makes difficult, or impossible? I mostly code in C# (which in my opinion is an excellent and productive ecosystem), but Rust seems to make a promise of providing a greater level of confidence in the code that is very alluring - I'd like to take the plunge, I'm just a bit worried about smashing into any hidden rocks at the deeper end
- kapolos 4y agoImho, Rust is not hard as in "high complexity hard", but it is hard as in "must practice to get good at". All the little rules and new concepts add up and in the beginning it can be frustrating to have the compiler complaining. On the plus side, it's like a motorcycle - it doesn't take long to drive it safely & efficiently. Learning Rust is fun, I wholeheartedly suggest it even if you don't end up using it at work.
- pitaj 4y agoI don't think motorcycles are a great analogy. They're way more dangerous for the driver than a car and you have to be constantly vigilant about your surroundings.
- vladvasiliu 4y agoYeah. If you start to get a bit too confident for your level, you'll just get yelled at by the compiler instead of ending up in the ER (if you're still breathing). But I suppose the analogy is more along the lines of "it can be intimidating at first, but you can quickly figure out how to use it reasonably well".
- kapolos 4y agoYou are right, I should have found a better one. I was between "bicycle" and "motorcycle" and I felt that bicycle would imply that learning is a breeze, so I went with motorcycle.
- protortyp 4y agoThis is really one of the easiest-to-comprehend articles I have read so far on Rust ownership! I hope they put out more similar content.
- mellosouls 4y ago(2021) btw in case any subsequent releases have changed anything.
- timakro 4y agoSmall correction: contrary to what the post claims this does not compile: fn main() { let s = String::from("hello"); foo(s); println!("{}", s); } fn foo(le_string: String) -> String { println!("{}", le_string); le_string } This does, which is probably what the author meant: fn main() { let s = String::from("hello"); let s = foo(s); println!("{}", s); } fn foo(le_string: String) -> String { println!("{}", le_string); le_string } Great post!
- kapolos 4y agoIndeed, thank you for catching it! I will make an update. Thanks again!
- aaa_aaa 4y agoIs it too much to ask for an article without memes, jokes and puns?
- kapolos 4y agoWhat can I say, I like memes & jokes ¯\_(ツ)_/¯ On a more serious note, I think lighthearted content is what's needed when it comes to intro stuff. There are no puns in the Rust Book ( https://doc.rust-lang.org/book/ https://doc.rust-lang.org/book/ ) ;)
- turtleyacht 4y agoThe Head First series uses similar techniques to help aid book learning. At least there is precedent :)
- fnordpiglet 4y agoWow. Yes, on the internet, it sort of is.
- oconnor663 4y ago> Traditionally, you either have to manage the memory yourself (à la C), or pass the burden down to a run-time feature of the language – heroically called “the garbage collector“. I think it's worth mentioning that C++ manages heap memory the same way Rust does, with destructors (Drop, in Rust). The story is a bit complicated with the new/delete operators only being deprecated recently and lots of legacy code sitting around, but vector and string work the same way, and those have been in common use for decades. The difference isn't the memory management strategy itself, but rather that Rust catches all our mistakes when we retain pointers to destroyed values. (Move semantics are also quite different, but that's a separate idea I think.)
- pjmlp 4y agoGood luck telling to most enterprise developers that new and delete are deprecated, specially when ISO C++ makes zero reference to it.
- harikb 4y agoRust fans, when you take a dig at Java or Python, be a bit nice and don't throw numbers without any basis > Python is two Orders of Magnitude (100x) slower than C. Suddenly, your $3,000 MacBook Pro barely beats an early-1990s-era 386DX computer. Right under Java logo > It is not just 60-frames-per-second games that long for high performance. Any CPU-bound or repeated process also requires it
- klysm 4y agoYou should be able to vaguely claim that Python and Java are inherently slower than Rust/C/Whatever. I agree claiming that it is 100x slower is a bit unfounded, but it's pretty easy to go find existing benchmarks that show results like this.
- kapolos 4y agoYes, it's exactly as you say. I did search for benchmark comparisons and bumped into one that had 2 orders of magnitude difference. But naturally, isolated benchmarks can't encapsulate the whole picture because there's a world of difference between testing how fast a loop runs and performance in production.
- codedokode 4y agoI guess that arithmetic operations are not very slow in Python, but if you use objects, then Python performs complicated lookups for every field or method access.
- kaba0 4y agoOne is not like the other. Java is much much closer to C/Rust than Python is to Java.
- kapolos 4y agoIf I recall correctly, the first time I used Java it was way back in JDK 1.1. It was amazingly slow. Of course, today's Java is fast enough for its domain, but I still enjoy throwing a jab at it here and there :)
- 29athrowaway 4y ago"Let's get rusty" on YouTube has a really good intro to Rust, including this topic and others. Short videos that are simple and go to the point.
- codedokode 4y ago> The biggest of said problems is that the garbage collector has the annoying habit to “pause the world”. I think that main problem of garbage-collected programs is that they usually allocate several times more RAM than they use. This causes excessive swapping which produces more noticeable lags than just garbage collection. I vaguely remember reading somewhere that the difference can be 6x, but I am not sure if this is the correct number. This means that garbage-collected software is great for large expensive servers stuffed with RAM sticks but not so great for a personal computer having just 4 or 2 Gb.
- pastaguy1 4y agoWould there be any value in a GC'd language where you had the option to explicitly call do_gc_now()? Is this functionality already out there and just not popular? I haven't spent a ton of time in these languages, so forgive the naivety. Again, naively, this seems like it would be a good compromise between what GC offers but near-determinism when you want it. I guess Rust and C++ also "pause the world", it's just predictable when it happens. It seems kind of arbitrary in some ways, like maybe I don't want the "world pause" at the end of this scope. There are ways around it obviously, but you start language-wrestling at that point.
- nicoburns 4y ago> Is this functionality already out there and just not popular? Yep. Node.js has this for instance (you have to enable it with a command line flag, but it's easy enough to do). I imagine most GC languages have a similar option.
- eloff 4y agoYou can often do this, although some runtimes treat it only as a hint and may ignore it (usually what you want). Go has some pretty strong guarantees on how long it will stop the world for, which makes it much less of an issue. But the best pause is still no pause, which means no GC.
- tsimionescu 4y agoVirtually all GC languages have this option, but it is rarely better overall than the default algorithm for deciding when to do it. The biggest problem with GC is that it is often global, and a simple call to invoke the global GC doesn't usually help from a local scope. There are some arena-based allocators (I believe OCaml's GC uses this strategy) where this may be more beneficial. But the question always looms: if there is not enough memory to serve an allocation invoked from a hot loop, what should the program do? Note also that in advanced GCs, such as .NET or the JVM, a hot loop that doesn't allocate will often not need to be stopped by the GC at all. There are even GCs (all proprietary as far as I know) that guarantee a fixed pause time, so that they can be used in real-time workloads - Azul's JVM GC has this option for example.
- 4y ago
- eternalban 4y agoThat kitty asked for "Pretty, please". Rust may be the wonder language but pretty it ain't.