> Rust is a multi-paradigm programming language focused on performance and safety, especially safe concurrency. Rust is syntactically similar to C++, but can guarantee memory safety by using a borrow checker to validate references. Unlike other safe programming languages, Rust does not use garbage collection. ## Tools and Frameworks - [[Cargo]] - [[Tokio]] ## Concepts - [[Ownership]] - [[Borrowing]] - [[Lifetimes]] ### Strong Considerations for Rust #### 1. Zero-cost abstraction You pay only for what you use. The runtime is based on what features you use for your program. What does this mean? For example, in Java or C#, every time you abstract code by introducing additional classes it adds overhead to your program. Because both languages have garbage collectors your extra layer of code needs to be cleaned after the program is done using it. This can significantly slow down your program. In Rust, high-level APIs will directly compile into machine code and can have as good performance as if you'd written lower-level code. #### 2. Immutable and Private by default When you define variables in Rust they are immutable by default. If you want the variable to be modifiable you need to explicitly state that by using `mut` keyword. The same goes for the data access modifiers. If you don't state that any field, function, struct, enum, or mod is `public` its access level is inherently limited to the local scope. Similar to what Java and C# does when you set class or function to `private`. What's the advantage of this? - A reduction in human error. Popular philosophy about how to set access modifiers is to maximize data protection. For example, if you know a field or function shouldn't be used outside of the class where it's defined it should always be set to private. When data is being modified or accessed where it shouldn't this can cause major bugs and security issues. Private by default push developers to be more conscious of their program design choices and helps to faster detect bugs. #### 3. Full Experience with Cargo and Rust Compiler When you set up Rust, you not only get Rust the programming language, you get a full environment. Rust comes with `cargo`, a user-friendly package manager. Cargo gives ease of managing dependencies and setting up build configurations. With only a handful number of commands, you'll be able to create projects, run projects, test them, and even upload to the [crates.io](https://www.bexxmodd.com/post/crates.io) where other Rust libraries and packages reside. Rust also comes with an amazing compiler. One of the first pieces of advice given to me when I started using Rust was not to fight the compiler. Instead, listen to it. Rust compiler utilizes LLVM's decades of optimization. However, besides that, it's very verbose and descriptive of what went wrong. It also suggests potential fixes and alternative approaches developers may consider to overcome given errors during compilation. If your code compiles it's highly likely that it will run without errors. #### 4. Safety first! no dangling pointers, no memory leaks, and no race conditions The core philosophy of the Rust language is to keep it safe without sacrificing performance. It may sound counterintuitive when you hear this first time (at least it was for me), but it's true. We already mentioned that Rust has no garbage collector. Instead, Rust uses what's called _lifetimes._ This not only allows code to run fast, but it also guarantees that no unused allocated memory lives in the program when its lifecycle is over. This exterminates the problems known to C developers like memory leaks and dangling pointers. Rust handles concurrent and parallel program design problems fearlessly. As Chapter 16 of Rust's official Book says: _"By leveraging ownership and type checking, many concurrency errors are compile-time errors in Rust rather than runtime errors. Therefore, rather than making you spend lots of time trying to reproduce the exact circumstances under which a runtime concurrency bug occurs, incorrect code will refuse to compile and present an error explaining the problem."_ If you want to learn more about how Rust solves this problem I'd suggest reading above mentioned [Chapter 16: Fearless Concurrency](https://doc.rust-lang.org/book/ch17-00-concurrency.html) #### 5. Rust is not JAL (Just Another Language) There are so many new programming languages getting born and getting hyped and then dying out namelessly. But not Rust. Graydon Hoare and the team at Mozilla gave much thought to the language design to solve many issues other popular languages have. This attracted lots of developers. Thoughtful conversations, suggestions, and contributions bestowed the language's growth in the right direction. Rust's community is very welcoming and offers immense support to the newcomers. The Rust's [Reddit](https://www.reddit.com/r/rust/), [Discord channel](https://discord.com/invite/rust-lang-community), and [Rust forum](https://users.rust-lang.org/) are amazing places to connect with other Rusteceans, ask for help, or seek advice. So I'll ask you: ## Notes - [[Cargo]] provides a solid package manager for the language as well some quality of life tools like the ability to create a project. - [[Correctness]] - Rust is often thought of as a language that favors correctness for the following reasons I’d give for why it’s easier end more ergonomic to write correct programs in Rust are: * exhaustive pattern matching * Result and Option types * builtin first class unit testing ability * builtin first class documentation ability ## Research ## Links - [Learn Rust](https://doc.rust-lang.org/book/ch00-00-introduction.html) - [Learning Rust through code reviews](https://loige.co/learning-rust-through-open-source-and-live-code-reviews/) - [My first cup of Rust](https://blog.frankel.ch/start-rust/1/)