Before we learn any more about the Rust Syntax there is one more thing that we should learn about and that is the Cargo Compiler that also comes with the rustc
that we use to compile the Rust program. But whats the difference between rustc
and cargo
. The short answer is that the rustc
is used to compile programs that are small and often are only one to some files big.
On the other hand, If you want to code a big project which also require some external packages in the production, then you need to learn about the cargo compiler.
Cargo
Well actually cargo is not a compiler but a build system and a package manager just like pip
in Python and npm
in NodeJs. It handles many things for you like compiling your projects and downloading dependency packages for you from the web on which your project relies on and so on. To start with cargo first check if have it installed on your system.
Linux>> cargo --version
cargo 1.51.0 (43b129a20 2021-03-16)
After that lets create a new project with cargo and to do that we run cargo with new <project_name>
as the arguments to it and it will creat the following files for us.
Linux>> cargo new rust_hello
Created binary (application) `rust_hello` package
Linux>> cd rust_hello
Linux>> ls
.gitignore Cargo.toml /src
In the /src
directory it holds all your rust files and currently it only has one file called main.rs
and in Cargo.toml
is where you specifiy the metadata for your project.
Cargo.toml
If you open Cargo.toml file now it should look like this.
[package]
name = "rust_hello"
version = "0.1.0"
authors = ["zainsci <60652827+zainsci@users.noreply.github.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
Here under the [package]
is all the info about your project and under the [dependencies]
you specify what packages you are using in your project.
Crates
Inside the Cargo.toml file and under the [dependencies]
we write down the package name in Rust we call them crates and the cargo will download it when we run the command cargo run
. It works just like pip
in Python and npm
in NodeJs.
You first look for crates in the crates repository on the Official Website. You look for what crate you want to use in your project, you look for install section and find the name and version no that they specified, copy it and then write down it in the Cargo.toml file just like i did below.
[dependencies]
rand = "0.7.3"
Now i can start using the rand
crate in our project.
Importing Crate In Our Projects
Importing modules in our file is just like you would do it in any other language. We use use
keyword followed by the <crate_name>::prelude::*;
and thats it.
We use the rand crate to generate a random number like this.
use rand::prelude::*;
fn main() {
let mut rng = rand::thread_rng();
let random: u8 = random.gen();
println!("Random Number: {}", random)
}
Running Cargo
Now that we have coded our project and want to compile it and see the result all we have to do is run the cargo run
command in the terminal and it will automatically run the compiled program for us.
Linux>> cargo run
Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 1.02s
Running `target/debug/playground`
238
Hope we learnd about what cargo is and how to work with it. Next time we will be taking a look at Rust Control FLow Statements like Loops and Ifs and Else.