Last week in my newsletter, I talked about the amazing language Rust - and why everyone needs to start learning it for 2024.
If you missed reading it, you can catch up on it here.
Rust is one of the fastest growing and highly performant languages of modern times.
Launched in 2015 for the public, Rust was meant to be a replacement language for systems level programming languages, and it has not failed to live up to its expectations.
So much so that Rust now has a fully thriving ecosystem built around it with many frameworks and libraries to support the development of apps across all platforms.
Why wait then? Letโs get started with setting up our first Rust project ๐
As we land on the Rust homepage, we see a compact, welcoming landing page with the latest release version mentioned on the right with the link to documentation in the Get Started button.
Once we enter the documentation, we have many different options available to start implementing Rust.
You can play around with Rust completely online with the Rust playground.
Letโs install Rust on our system. Open up your terminal and run this curl command -
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Once you run this command, you will be presented with a welcome message along with the location of where Rust toolchains and metadata, cargo
the package manager will be installed, and the information about the PATH variables added, like so -
Once you โEnterโ to approve the installation, the process will be completed and you can test it by checking the cargo version -
Building the first Rust project -
Now, as mentioned, cargo
is the official package manager for Rust and it is very handy. From accessing the documentation, help pages, to creating a new Rust project right from the CLI, cargo is very useful. Here are some quick commands for reference -
You can also type cargo โhelp
to see a comprehensive list of commands that come with cargo for quick navigation -
With that done, letโs create our first Rust project -
In your terminal, cd
into the folder/directory where you want to create your new Rust project and run the command - cargo new learn-rust
Open this project folder in your code editor. You can see that the project has a few starter files generated by cargo
command -
Inside the src
folder, we have main.rs
file where .rs
is the extension for Rust files, and then a Cargo.toml
file which is the configuration file for the project. It is similar to YAML and JSON files, but just a bit easier to read due to its simpler semantics. This file is what we call the โmanifestโ file for Rust. It contains the metadata and dependencies for the entire project. This sounds similar to the package.json
file in ReactJS/NodeJS projects, doesnโt it?
The main.rs file is where we write all our application code. Again, similar to index.js file.
Weโll come back to these lines of code and their meanings very soon. Letโs first run our project.
We use the command cargo run
to generate the output of our main.rs
file above. Once we do, we get the following output -
You can see the output โHello, world!โ in the terminal ๐ฅณ Congratulations, you just ran your first Rust program ๐
You can notice that a new folder target has been created and this is where our debug files, logs, and output file will be stored.
And that is it for this tutorial. In the next blog, weโll learn more about cargo
package manager and the magic it weaves โจ
I have also published a detailed video on this topic in my YouTube channel - watch it here.
Subscribe to my weekly newsletter on software development, my journey with code, and many other useful information to elevate your programming career.
Top comments (0)