Rust first steps
02 April, 2023 - 2 min
Getting Started with Rust and Cargo
Rust has been on my queue for a bit, since I decided to get into web3. Rust is a fast, reliable, and efficient language that's perfect for building web applications, command-line utilities, system software, and more.
In this post, I'll go through the basics of setting new Rust project using Cargo. It's a way to help me structure and solidify what I've learning from the official documentation.
note: guide is curated for macos.
1. Prerequisites
Before we start, make sure that you have Rust and Cargo installed on your system. If you don't, you can download and install Rustup, which includes both Rust and Cargo.
$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
You can verify that Rust and Cargo are installed by running the following commands:
$ rustc --version
$ cargo --version
2. Creating a new project with Cargo
To create a new Rust project, open a terminal or command prompt and navigate to the directory where you want to create the project. Once you're in the desired directory, run the following command:
$ cargo new hello-rust
The new
command creates a new Rust project named project-name
with a binary target. You can also create a library project by using --lib
.
Once the project is created, navigate to the project directory using the cd
command:
$ cd hello-rust
3. Building and running the project
To build the project, run the following command:
$ cargo build
This command compiles the project and generates an executable file in the target/debug
directory. You can run the executable using the following command:
$ ./target/debug/hello-rust
Alternatively, you can use the cargo run
command to compile and run the project in a single step:
$ cargo run
4. Testing the project
You can test your project using the following command:
$ cargo test
This command runs all the tests in your project's src
directory.
5. Conclusion
Congratulations! You've learned how to create a new Rust project using Cargo. Cargo makes it easy to manage your Rust projects, and with Rust's powerful features, you can build high-performance applications and system software with ease.
I hope you find it helpful!
Hope you enjoyed! Vicente