dwyl / learn-rust

:bowtie: learn rust-lang to build systems.
GNU General Public License v2.0
11 stars 1 forks source link

learn rust

Learn rust-lang to build systems.

Why use Rust?

Blazingly Fast

Rust does not use garbage collector for memory management, and this means that you do not need to manually manage memory! You can see some benchmarks to compare Rust with C and other languages in this post, for exemple

Most Loved Language!

According to Stackoverflow's 2022 Developer Survey, Rust is on its seventh year as the most loved language with 87% of developers saying they want to continue using it. Rust was the most wanted language too!

Memory Safety

Rust is a blazingly fast, memory safe, and concurrent programming language that runs without garbage collector, and how Rust does it?

Rust uses Ownership models to ensure memory safety. The ownership model is a set of rules that the compiler checks at compile time. The rules are:

Ownership is a new concept for many programmers, it does take some time to get used to. The good news is that the more experienced you become with Rust and the rules of the ownership system, the easier you’ll find it to naturally develop code that is safe and efficient. Keep at it!

You can see the power of the ownership model in some cases. You can read how this is useful and how discord uses rust speed without garbage collector in their blog post.

Cargo

When you code in other compiled languages like C you have to compile the program, insert a name and run, like you would do with the Rust Compiler:

$ rustc hello.rs
$ ./hello
Hello, world!

It will be boring to do this every time you want to run your program. Furthermore, most non-trivial programs will likely have dependencies on external libraries, and will therefore also depend transetivily on their dependecies. We can avoid the manual tedium involved by using the rust Package Manager.

Cargo is the rust packager manager. Cargo aims to do the following:

You can use cargo not only to build your own projects, but also to download and build other people’s projects. Cargo is the build tool that Rustaceans use most often, and Cargo’s conventions make it easy to use other Rustaceans’ code as well.

$ cargo run
    Compiling hello v0.1.0 (file:///path/to/hello)
     Finished dev [unoptimized + debuginfo] target(s) in 0.27s
      Running `target/debug/hello`

This simple commands runs and compiles the program. It also creates a Cargo.lock file that contains the exact versions of the dependencies that were used to build the program. This is important because it means that the next time you build the program, Cargo will use the same versions of the dependencies that it used the last time. This is important because it means that the next time you build the program, Cargo will use the same versions of the dependencies that it used the last time.

These command runs in the default binary project. You can also create a library project with the --lib flag:

$ cargo new hello --lib

This will create a src/lib.rs file instead of a src/main.rs file. The src/lib.rs file is the crate root of a library crate, and the src/main.rs file is the crate root of a binary crate.

Cargo turns your life easier by providing a lot of useful commands. You can use cargo build to compile your project without running it. You can use cargo check to check if your project compiles without actually compiling it. You can use cargo doc to generate the documentation for your project. You can use cargo test to run the automated tests that are in your project. You can use cargo bench to run the benchmarks that are in your project. You can use cargo update to update the dependencies of your project. And many others! If you want to know more about cargo, you can check the cargo book

And more...

Atwood's Law states:

Any application that can be written in JavaScript, will eventually be written in JavaScript.

We've been there and seen the appeal of JS-everywhere. And then subsequently felt the pain of having to debug and maintain complex JS codebases in large teams.

Seriously, open the DevTools of your Web Browser and remind yourself of the "quirks" of JS:

javascript-quirks

Many teams have opted to use TypeScript to paper over the cracks in JS by having static typing, but it's a bandaid [on a bullet wound] at best. 🩹 Better than nothing ... but the underlying engine still allows people to write un-safe code and there are still errors discovered by "end-users" when TypeScript is used.

Even with the TS compiler you can still end up with "undefined is not a function" in your production code: https://stackoverflow.com/search?q=typescript+undefined+is+not+a+function

typescript-undefined-is-not-a-function

Note: we are speaking from multiple years of experience with writing TypeScript in large teams. Most recently a ReactNative App for a FinTech company that was written in TS and executed TS Lambda functions on the backend ... This is fairly typical "Full Stack" these days. It was a nightmare to debug. 😱 And the management answer was to just throw more people at the problem. There's a better way.

Why not just use a language that solves all of these issues and have resulting programs that execute at least twice as fast?

Eventually, all code will be written in a type-safe high performance programming language that is maintainable with the help of a friendly compiler.

We wish we made the switch sooner! ⏳

What? 💭

Rust is a multi-paradigm, high-level, general-purpose programming language. Rust emphasizes performance, type safety, and concurrency. ~ https://en.wikipedia.org/wiki/Rust_(programming_language)

This is a pretty good textbook definition. But it doesn't tell me anything about the benefits to me! 🤷‍♂️

What's in it for Me?

"In other languages it's easy to start projects, in Rust it's easy to finish them" ~ No Boilerplate "Stop Writing Rust" - https://youtu.be/Z3xPIYHKSoI

When referring to popular Rust projects that haven't been updated in years, Tris observed: "They are not abandoned, they are done."

This is a breath of fresh air to anyone who has had to maintain code in any other language.

How? 👩‍💻

Concepts

Rust has many concepts that are not present in other languages but you need to get your hands dirty to understand them. Here some of them:

image

Exercism is a great place to start learning Rust, they won't take your hands and show you how to solve the exercises but they will give you the base concepts and you will have to figure out how to use them. For me personally, it was the best way to learn Rust.

Books

Rust has books for almost everything, from the basics to the most advanced concepts.

I will leave some books that you will find useful:

Installing

https://doc.rust-lang.org/book/ch01-01-installation.html

curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh

Note: as a security-conscious person I don't feel super comfortable runnint an arbitrary script on my computer like this ... but rustup.rs is the official way to install Rust, Cargo, etc: rustup-is-official

Communities:

Background Reading