- Why Rust? Itās important to answer this question to motivate our learning
- Systems/low-level programming offers benefits like faster runtimes, but itās incredibly hard to work with because of subtle bugs that come from things like memory-safety issues
- Rust avoids these bugs with a āsmartā compiler that refuses to compile code with elusive bugs
- This allows devs to focus on program logic rather than chasing down bugs
- Rust also brings contemporary developer tools to the systems programming world:
- Cargo: a dependency manager that makes adding, compiling, and managing dependencies seamless
- Rustfmt: a linter that ensures consistent coding style
- Rust language server: a language server that powers code completion/inline error messages on IDEs
- In a nutshell, Rust just makes low-level programming much more straightforward and approachable
- This is why it has such a big fanbase
- Rust is an ahead-of-time compiled language, which means that you can make an executable (via something like
rustc main.rs) and give it to someone else. They can then run it without having Rust itself installed- Other languages (like Python) donāt require compile-run cycles and instead have only 1 command, but require language support for someone to run programs
- Trade-offs!
- Three main commands to know when working with Cargo, Rustās built-in dependency manager
cargo buildcreates an executable file in thetarget/debugsubdirectorycargo build --releaseis what you should run when youāre building for release, not development- Benefit is that it adds optimizations to make your executable run faster, but these take more time to apply (so not optimal for quick testing)
- Places executable in
target/releaseinstead oftarget/debug
cargo runhandles building and running all in 1 command, so you donāt have to find and run the executable filecargo checkjust checks that there arenāt any compilation errors ā we can call this periodically as weāre coding to check ourselves
References
- Chapter 1 of The Rust Programming Language by Steve Nichols and Nicole Klabnick.