15 ms·
Welcome to Comprehensive Rust
- incrudible 4y agoThe first thing I implore everyone to do when evaluating Rust is to check out a non trivial project and start hacking on it. I have always found the compiler to be unacceptably slow in these cases, especially if you come from C rather than C++. If you work your way up from Hello World you may not ever notice this until you already invested a substantial amount of effort.
- Gwypaas 4y agoI have never found this to be the case as long as you are using workspaces to split up the compilation units.
- rowanG077 4y agoCould you point me towards a project where this is the case? I want to try it out as I never found Rust slow to compile.
- deaddodo 4y agoRust is most definitely slow to compile compared to GCC or LLVM for C/C++. This is a known situation and has been since the pre-1.0 days because of the heavily front-loaded macro and borrow systems. It's the tradeoff you have for having a "safe" language vs a "trusting" one. I would argue that it's not as bad as it's made out to be, especially if you're not doing full recompiles and using it on modern (2019+) hardware; but it is a noticeable difference in similar large codebases.
- rowanG077 4y agoIt's slower then C for sure. But slower then C++? I doubt it.
- est31 4y agoRust is most comparable to C++ with heavy TMP, as it itself also makes use of templates.
- kibwen 4y agoTo be precise, Rust generics perform monomorphization, which C++ templates also perform. But I wouldn't go so far as to compare the language to "C++ with heavy TMP", as that's far too broad of a characterization; TMP implies a lot of things, e.g. poor error messages, that Rust doesn't necessarily exhibit.
- est31 4y ago> TMP implies a lot of things, e.g. poor error messages, that Rust doesn't necessarily exhibit. That's fair. My comment was mostly about performance.
- pjmlp 4y agoC++ is only slower in heavy template metaprogramming. Additionally thanks to heavy reliance on binary libraries, when starting a new project, we only need to compile our own code.
- est31 4y agoRust macros are not the fault for its slowness. expansion is one of the fastest compilation phases. Time is rather spent in other steps like type checking, llvm optimizations and such. The issue is more Rust's generics as they cause a lot of code to be passed to llvm (among other issues for Rust's slowness).
- Arnavion 4y agoMacro expansion is slow, but only noticeably in the specific situation of a) third-party proc macros, b) a debug build, and c) a few thousand invocations of said proc macros. This is because building in debug mode also compiles proc macros in debug mode, so while the macro itself compiles quickly (because it's an unoptimized build), it ends up running slowly (because it's an unoptimized build). I know this from observing a ~90s difference between debug and release builds, of a large (mostly auto-generated) crate that had a couple of thousand `#[derive(serde::*)]`s. [1] This doesn't affect most users, because first-party macros like `#[derive(Debug)]` etc are not slow because they're part of rustc and are thus optimized regardless of the profile, and even with third-party macros it is unlikely that they have thousands of invocations. Even if it is a problem, users can opt in to compiling just the proc macros in release mode. [2] [1]: https://github.com/Arnavion/k8s-openapi/issues/4 https://github.com/Arnavion/k8s-openapi/issues/4 [2]: https://github.com/rust-lang/cargo/issues/5622 https://github.com/rust-lang/cargo/issues/5622*
- est31 4y ago> Macro expansion is slow, but only noticeably in the specific situation of a) third-party proc macros, b) a debug build, and c) a few thousand invocations of said proc macros. Oh that's interesting. But note that it's an issue caused by (from the perspective of the language) user code being slow, and also only if you compile in debug mode. You probably don't want to unconditionally compile proc macros in release mode, as most times they probably don't get invoked thousands of times. Ideally you would probably a) invoke macros in a parallel fashion, with multiple expansion threads, and b) maybe have rustc signal to cargo somehow that there are tons of invocations of a specific macro and that it might make more sense to recompile the proc macro with optimizations turned on.
- ergonaught 4y ago“I’ve never been bothered by the compilation speed” is not the same thing as “It isn’t slow to compile.”
- rowanG077 4y agoSure! I didn't claim anything close "it isn't not slow to compile" and I'm not sure how you could interpret my comment like that. Notice how I clearly asked to be educated.
- nicoburns 4y ago> I have always found the compiler to be unacceptably slow in these cases, especially if you come from C rather than C++ I’ve found this to be highly dependent on the hardware you are using. On my 2015 MacBook Pro, Rust was slow enough to be annoying. On my new M1 Pro MacBook (which is ~9x faster), it’s fast enough for me not to notice compile times.
- rapsey 4y agoSlower compile times are a small price to pay for memory safety and much higher productivity.
- saagarjha 4y agoSlow compile times can reduce productivity.
- rapsey 4y agoNot nearly as much as chasing bugs that do not happen in Rust.
- saagarjha 4y agoThat would depend on what you're doing, of course :)
- rapsey 4y agoNo not at all.
- saagarjha 4y ago…perhaps you lack experience of a time where fixing bugs that cannot be effectively discovered using tools available in other languages took less time than the equivalent code in Rust would take to compile?
- rapsey 4y agoYet all the tools that exist still don't prevent memory safety bugs found regularly in pretty much any C codebase. Using safe rust will be a productivity boon because of its type system, crate ecosystem and memory safety.
- 4y ago
- pen2l 4y agoLook into Apple silicon - lots of cache, great branch predictions, pointer-chasing prefetchers and more to subside just about all of your compiling worries.
- saagarjha 4y agoHmph, I wish…
- wongarsu 4y agoOn the other hand, I've always found it surprisingly easy to dive into non-trivial rust projects and start contributing, at a speed that would be unthinkable in C or C++. So I second the advise for non-trivial projects, they give a much better idea of the upsides and downsides of rust.
- 9dev 4y agoIs that really due to the language or that we collectively have become better in structuring software projects?
- Sharlin 4y agoYou can trust the compiler to yell at you when you break many invariants that would be tacit and implicit in C and even in C++ (even with best practices followed) which can make you much more productive when you’re just familiarizing yourself with a codebase. Also, the two aren’t necessarily mutually exclusive. Many people learning the language have reported being surprised by the way Rust seems to make "doing things right" the path of least effort, and this applies to best practices in general, not just memory safety.
- db48x 4y agoI’ve had that experience. I had to write code for a complicated reference counted datastructure. The code was a mess at first because I was approaching it the wrong way, but once I understood it well enough to write it correctly all the pieces fell into place. Extraneous complexity was eliminated, and all the possible mistakes turned out to be impossible. The resulting code is so simple and straight forward that anyone can read it and see that it is correct. And as is traditional, the first time it compiled it was correct in all respects.
- the__alchemist 4y agoI'm suspicious of this as well. It's common for OSS C and C++ projects (at least in embedded) to be mostly undocumented / few comments, have no easy way to debug by printing to console, and have a complicated build process that requires a certain OS and setup. Top doc comment: The same license info that's in every file. Not helpful for describing what the module does and how it fits in with the rest of the project.
- modernerd 4y agoI class Bevy as a “non trivial” project. It's also an example of how to immediately set new Rust devs up with better system performance to reduce the problem you mention where compilation becomes a bottleneck as a project grows. https://bevyengine.org/learn/book/getting-started/setup/ https://bevyengine.org/learn/book/getting-started/setup/ The early setup docs offer ways to improve compile speeds. Some help all Rust projects (change your linker) and others are specific to Bevy (enable dynamic linking if you're not on Windows). Yes, it's a little jarring for an intro to game development with Rust to start with, “first, change a bunch of things so your compile speeds don't suck by default”. But I appreciated it because it helps you evaluate Bevy properly (it's as fast as it'll get from that point) and it also made my non-game Rust project workflows faster. Bevy also sets expectations well about the initial slow-ish build (“This will take some time as you are essentially building an engine from scratch. You will only need to do a full rebuild once. Every build after this one will be fast!”).
- vvanders 4y agoYeah Bevy does a fantastic job here(which given the focus on iteration time for gamedev makes sense). With the dynamic link feature it takes about 1s to compile + link and Bevy is very much a nontrivial runtime/library that makes use of lots of Rust features.
- demindiro 4y ago> have always found the compiler to be unacceptably slow in these cases How slow is "unacceptably" slow? Personally I do not find this to be an issue the vast majority of the time. Especially in combination with cargo watch which can automatically run a check/build/tests/... whenever a change is made.
- kibwen 4y agoI work on quite a large Rust project, and my regular `cargo check` takes around 5 seconds. Of course, I'd be happy if it were instantaneous, but "two deep breaths" worth of turnaround time is hardly a deal-breaker, especially given all the bugs that the compiler prevents for free that I don't need to spend time tracking down. EDIT: I checked just now and it takes one second to `cargo check` the shallowest crate in our workspace, and six to check the deepest.
- sanderjd 4y agoI think this is good advice! But YMMV; I've also always found the compiler to be slow, but acceptably so for what it's giving me.
- WuxiFingerHold 4y agoSplitting your projects in smaller packages by using workspaces helps a lot reducing the compile time.
- 1f60c 4y agoIs this “just” a 20% project or is this more significant (like Android one day supporting Rust to develop apps)? And I haven’t looked at everything yet, but it suggests to install Rust like this: sudo apt install cargo rust-src While everyone I know uses rustup^: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh ^ https://rustup.rs/ https://rustup.rs/
- schipplock 4y agoThey mention rustup on the 3rd slide.
- rob74 4y agoThey're using Rust in the android system: https://security.googleblog.com/2022/12/memory-safe-languages-in-android-13.html https://security.googleblog.com/2022/12/memory-safe-language...
- est31 4y ago> Is this “just” a 20% project or is this more significant (like Android one day supporting Rust to develop apps)? Android already uses Rust internally, in fact for new code it's a major language. They have made this guide to teach Rust to their engineers working on Android, according to this post on reddit: https://old.reddit.com/r/rust/comments/zrs1of/new_rust_course_by_android_comprehensive_rust/j14dgu3/ https://old.reddit.com/r/rust/comments/zrs1of/new_rust_cours...
- ohbtvz 4y agoI'm tired of installation instructions that consist of "curl | sh". Especially since this one just tries to detect the platform and downloads the correct installer for me. I know what platform I'm running, and I don't want to pipe the internet to my shell. In the end this is unpacking a tarball in ~/.rustup, I don't need the risk of running some bespoke script for that.
- est31 4y agoOn some distros you can install rustup directly, e.g. NixOS or Arch. For Ubuntu, it's sadly not in the default repositories, but there is a snap for rustup uploaded by the rustup maintainer. https://pkgs.org/search/?q=rustup https://pkgs.org/search/?q=rustup https://snapcraft.io/rustup https://snapcraft.io/rustup
- omniscient_oce 4y agoFantastic timing! I was just going to start looking into running Rust on Android
- shivekkhurana 4y agoCan Rust make Android apps faster ? It feels logical because Rust is close to the metal, but I don't know enough Android or Rust to be sure.
- saagarjha 4y agoRust can make Android apps faster, but so can Python or Java or JavaScript or C++. It's generally not useful to make a comparison like this in a vacuum.
- mrtngslr 4y agoHi, I wrote the new course. We have been enabling the use of Rust in the Android Platform. Roughly speaking, the Android Platform is the Linux distribution running below the Android apps we all know. There are a number of daemons running on the system and these are often written in C++. We've now made it possible for Android engineers to write them in Rust instead (or to link in Rust libraries if they want). To do this, the Android build system had to be extended with new rules, see https://source.android.com/docs/setup/build/rust/building-rust-modules/overview https://source.android.com/docs/setup/build/rust/building-ru.... The overall goal is to have more secure software. Rust removes a whole class of possible security vulnerabilities and we deploy it to make phones more secure. See https://security.googleblog.com/2022/12/memory-safe-languages-in-android-13.html https://security.googleblog.com/2022/12/memory-safe-language... for details on that.
- deleted 4y ago[deleted]
- pjmlp 4y agoAndroid apps are already compiled to native code, being faster or slower is a matter of developer skills, and how much they paid attention to algorithms and data structures lectures.
- w4rh4wk5 4y agoWhy would one use this tutorial over the main https://doc.rust-lang.org/book/ https://doc.rust-lang.org/book/ ? Apart from the Android specific additions I mean.
- saagarjha 4y agoFrom another comment: https://old.reddit.com/r/rust/comments/zrs1of/new_rust_course_by_android_comprehensive_rust/j14dgu3/ https://old.reddit.com/r/rust/comments/zrs1of/new_rust_cours...
- mrtngslr 4y agoThanks for reposting :-) In general, I feel that the new course will be useful for people who want to teach Rust. If you have a group of engineers and you want to teach them Rust, then this is a ready-to-go solution. You only need to spend some time getting familiar with the material and then you can start teaching it. I don't think there was such a resource before. For self-study, I really like the Rust book and also Rust by Example. I've listed a bunch of good resources here: https://google.github.io/comprehensive-rust/other-resources.html https://google.github.io/comprehensive-rust/other-resources.....
- saagarjha 4y agoBased on my personal experience, I think it might also be helpful to have an even shorter version tailored to "Rust for C++ programmers", which I find to be useful because you can mostly just go "ok here's the syntax for basic things, here is how lifetimes work and what problems they solve, here are the replacements for things you are familiar with and why some operations are not available for them". In particular, this lets you skim over "What is the stack? What is the heap?" that many introductions will spend time on.
- mrtngslr 4y agoYes, it's definitely important to tailor the training to the audience! When I'm teaching the course, I start by asking people about their background — if it's primarily C/C++ people, then we can quickly page through the slides about the stack and the heap.
- warabe 4y agoIt's kind of surprising to see that Rust is so rapidly accepted by wide range of developers. Unfortunately I'm from DataScience field, so I cannot see much motivation to learn Rust, but I am considering learning it, because language itself seems exciting! Is there anyone on HN who is from DataScience field like me and has learned Rust? It would be much appreciated if you could share the experience.
- toyg 4y agoRust is a safer C/C++. If you'd never consider writing anything in C/C++ (i.e. low-level code where performance is essential), you'll never use Rust.
- FridgeSeal 4y agoThat’s underselling it quite a lot. Rust has a number of features (ergonomic and technical) and some really nice language design that permits writing quite “high-level” code. Personally, having a compiler and type system as powerful as Rusts makes it worthwhile alone.
- bitexploder 4y agoThis. Rust is quite pleasant to use and once some barriers are overcome quite productive. Not Python fast dev speed, but easily Go levels of productive for me. I rarely fight the borrow checker unless building tricky data structures. It will still stump me once in a while with lifetimes, but the trade off is so worth it to have memory safety guarantees and know there are no foot guns like C code would have. And if you are just cranking something out you can write code a little less idiomatically and of efficiently for quick scripts or smaller throwaway CLI tools. Rust prefers a more functional style but you can just as easily make it not so.
- pjmlp 4y agoMost of those features are available in ML derived languages, without having to deal with Rust features that make it usefull C and C++ replacement. With faster compiler toolchains and REPL like tooling.
- deleted 4y ago[deleted]
- eggy 4y agoI still play with Rust (and Zig), but I have decided to put my work efforts into SPARK[1], the subset of Ada, for high-integrity software and formal verification. I know AdaCore and Ferrous Systems are collaborating in trying to bring a lot of Ada/Spark's capabilities to Rust, but this is still going to be some time. Ada has a longer legacy in this game. I am working on safety critical control systems and there is a lot to prune from existing work in this area in SPARK and Ada. This book[2], "Building High Integrity Applications with SPARK" really sold me on the benefits. It is a great intro of how SPARK was used for the CubeSat program at Vermont Technical College (VTC). A CubeSat was successfully launched in 2013. I could not find any critical mission software written in Rust yet especially that far back. I found Julia's selection for a flight collision avoidance system perplexing[3], but it proves a PLs syntax and evangelism (maybe not in this case), can bias a selection of a PL for such tasks. People find Ada/SPARK's syntax and methods verbose and similar to Pascal or other PLs, however, I was up and running quickly in it as opposed to Rust. You can program to bare metal in SPARK, and the tools are already there to provide high-integrity software. The tools for SPARK automate most of the verification. Most of Rust's wishlist is already in SPARK (and Ada). I hope the Ferrous Systems and AdaCore collab bears fruit for Rust, but I have been programming since 1978, and I play with many PLs, but I found Rust's initial learning curve very off putting and I don't have the time to wait for it to mature to SPARK's level. And this is from someone who loves J/APL/BQN and Lisp, so I have no issue with different programming paradigms. I found Zig much easier to get things done from the start, although it does not strive to be a SPARK or Rust. [1] https://www.adacore.com/sparkpro https://www.adacore.com/sparkpro [2] https://www.amazon.com/Building-High-Integrity-Applications-SPARK/dp/1107040736 https://www.amazon.com/Building-High-Integrity-Applications-... [3] https://juliahub.com/case-studies/lincoln-labs/ https://juliahub.com/case-studies/lincoln-labs/
- pjmlp 4y agoNVidia also went through a similar decision for their high security critical firmware.
- celurian92 4y agowhat resources can you suggest to learn formal verification and implement it to make high integrity fault tolerant services? I am quite intrigued by AdaCore. Want to try it out. I dont know much about Formal Verification.
- abiro 4y agoThis is great. I'd recommend the Learn Rust With Entirely Too Many Linked Lists [0] tutorial as the next step. It really helps you grok the borrow checker. [0]: https://rust-unofficial.github.io/too-many-lists/index.html https://rust-unofficial.github.io/too-many-lists/index.html
- mrtngslr 4y agoYeah, that is a fun tutorial indeed :-D I forgot to add that one to the list of https://google.github.io/comprehensive-rust/other-resources.html https://google.github.io/comprehensive-rust/other-resources..... Would you care to open a PR for that?
- nf17 4y agoUnrelated: but why does Google use Kotlin(for developing Android apps) and Rust when they have a nice little language in Go. Genuinely curious: Is Go not a good fit for developing Android apps(Is targeting JVM the concern?) or system programming(using Rust in Android systems), what does it lack?
- grishka 4y agoMost system APIs are only exposed in the JVM environment without native equivalents. You do need a JVM language to do anything meaningful. Either that, or you'll have to use JNI.
- deleted 4y ago[deleted]
- optymizer 4y agoI dont have first hand knowledge, but I've been in the space for 10 years and wrote Go, Java, Kotlin, so here is my take. Kotlin made a huge effort to stay compatible with Java. You can call Kotlin code from Java and Java code from Kotlin, so orgs could migrate their apps piece by piece. The only real incompatibilities that I've experienced were when going from C code via JNI into Kotlin. All sorts of really weird issues creep up. There was no boiling the ocean in any of the large apps I worked on at Amazon and Meta - just add Kotlin and see how it goes, it's a really easy sell to tech VPs. First we added Kotlin just for the unit tests, then for small features and eventually with Google releasing Jetpack with Kotlin extensions, for all new apps and features. Kotlin also improved upon existing painpoints in Java and provided a ton of syntactic sugar for things that were tedious in Java. For example, proper get/set syntax for fields, data classes with autogenerated equals/hashcode, explicit nullability for types, val with 'final' semantics to get rid of all the final variables, fields in interfaces, top level functions, etc. All the functional stuff in it is nice too. Go is much leaner than Kotlin. The VM or performance is not an issue - Kotlin generates entire classes for simple syntax, to make things convenient and compatible, but the bytecode is pretty bloated. I imagine that Google had sufficient resources to make a Go<->Java translation layer (though Go would have needed generics) but transitioning a huge API like Android is already a large effort, and if the language isn't fully compatible with the existing code, it would have added an order of magnitude of complexity, so Kotlin was a much easier sell. In my opinion, migrating to Dart would have been more practical than Go (as evidenced by Flutter), so thats the real head-scratcher. It would have been easy for Java devs to learn Dart because the OOP concepts translate directly. Other factors: Google already had a relationship with JetBrains because of Android Studio. The Oracle Google lawsuit was happening at the time and I speculate that Google needed to show to Oracle they can move off of Java. Kotlin happened to be available.
- neilv 4y agoAnyone happen to have this in EPUB or PDF format (for an ebook reader)?
- mrtngslr 4y agoThe course is meant to be interactive, which means that you should try out the embedded code snippets. You can edit them and run them from your browser :) However, if you really want a PDF, then use the print page[1]. It will let you produce a ~210 page PDF with the entire course. For learning Rust via a non-interactive medium, I recommend the Rust Book[2]. It has all the narrative that the course material is missing. [1]: https://google.github.io/comprehensive-rust/print.html https://google.github.io/comprehensive-rust/print.html [2]: https://doc.rust-lang.org/book/ https://doc.rust-lang.org/book/.
- mrtngslr 4y agoThanks for posting the link and thank you very much to everyone who have sent us PRs over the last 24 hours! Please keep submitting them. You can use the little pencil icon in the top-right of any page to quickly submit a typo fix patch. I'm from Denmark and English is not my first language — I very much appreciate the help from you to fix all the grammar mistakes :-)