This is the first in a series of articles to help developers understand Rust better, I will be covering most of what you can find in The Rust Book, in a more synthesized and concise way. I will start with the basics and build my way up to more complex topics.
Disclaimer
This course is intended for people who have a basic understanding of programming languages, preferably those who have written code before, this doesn’t mean you can’t follow along if you have no experience, just that it will be harder to understand some concepts.
Don’t get discouraged if you fail to understand the concepts presented in these articles, I’d like to encourage you to ask questions, if you need assistance you can send me a message.
Try Rust
If you’re not comfortable installing Rust, you can try Rust in the browser!
Installing Rust
If you use Windows, you can download the installer here.
Install Rust by pasting this in your terminal if you’re on a UNIX based system
You will be presented with the following
Type your desired method of installation and let the installer do its thing. Once it’s done, open a new instance of your terminal and type rustc
, you should see something like this
What just happened?
You just installed Rust, the installer doesn’t only install the compiler, it comes with a set of tools that will help you out in your journey as a Rustacean, I’ll explain what they all do.
- rustc -> This is the Rust compiler, it takes the code you’ve written and turns it into machine code, making it understandable by your machine and therefore, executable.
- rustup -> This tool manages Rust, it can update and remove the tools or personalize your preferences.
- cargo -> Cargo is Rust’s package manager, it manages dependencies for projects and can help you create new projects from scratch.
- clippy -> Clippy is a collection of lints to catch common mistakes and improve your code. Linters are static code analysis tools used to flag various kinds of programming errors.
- rust-docs -> Is a tool used to generate documentation for your code, this saves you a lot of time.
- rust-std -> This is the Rust Standard Library, a collection of crates you have available when writing your programs. Every Rust program is called a
crate
. - rustfmt -> A tool for formatting Rust code according to style guidelines. This one is pretty cool.
Managing Rust
There are various things **rustup ** can do for you, such as update rustup update
and uninstall Rust rustup self uninstall
. We will discuss more this tool in a future article.
Write Your First Program
Let’s get coding! Start by making a directory for your files.
Inside that directory, create a file by either touch main.rs
and opening it or using your preferred text editor.
And write the following:
fn
is the keyword for defining new functions and main
is the name for that function, also the entry point for our program. println!
is a macro, not a function, we will learn more about macros in a future article but for now, know that println!
will print “Hello, world” to the terminal.
Compiling And Executing
It’s time to run our program, save your file and exit your text editor, we will use rustc
to make a binary of our file.
You will notice a new file with the name main
was created, this is our binary file, to run it type:
You should see Hello, world
on your screen.
Congratulations! You’ve successfully written, compiled, and ran your first Rust program.
Conclusion
Rust’s main objective is to create a language that empowers everyone
to build reliable and efficient software, in the next articles, we will learn how to make use of Rust’s best features, such as cargo
, you can check the next article.
If you enjoyed this article, consider sharing it so more people can benefit from it! Also, feel free to @me on Twitter.