Rust for beginners — Part 2 — Rust compiler, Cargo and Write your first piece of rust code
Let’s write our first piece of code in rust !
Note: Kindly have a look into the previous section of tutorial to understand about introduction and installation of Rust
Previous section: https://medium.com/@manikandan96372/rust-for-beginners-part-1-introduction-and-installation-76e80624930c
Create your project folder as “rust_fundamentals” or any name of your choice and add a new file called main.rs to the root folder of your project and add the following snippet to it
fn main() {
println!("Hello World !");
}
Now open your terminal and run the following command to compile your rust code
rustc main.rs
Running this command should compile your “main.rs” file and create another file on the same name called “main”
So what does this “rustc” does to your code ?
“rustc” is the rust compiler that compiles your rust code to an executable binary code.
Now you know how to compile your code, but how to run it ?
Run the following command in your terminal using the “main” file that was created by “rustc”
./main
This should give you the following output in your terminal
Hello World !
HOORAY !!! you have written your first piece of code in rust and its working !!!
But wait, isn’t it tiresome to compile your rust code and then run the executable binary every time ? wouldn’t it be easy if you could compile and run it in a single shot ?
Yes, it is possible using “CARGO”, cargo is a rust’s build system and package manager ( like how NPM is for node.js ). Cargo helps you to compile your rust code, compile your packages and upload them to crates.io, which is a registry where you could find your rust packages.
How to use it ?
Run the following command in your terminal to initialize cargo in your project
cargo init
Once you run the above command, it should create a new file in your project folder called “cargo.toml”
cargo.toml — this helps you to declare your dependencies of rust packages, once you declare the dependencies that are required for your project, cargo will extract the necessary details about the dependency and add it to your cargo.toml and related build information to cargo.lock file.
Run the following command in your terminal to compile and run your file using cargo
cargo run
Once the above command is executed, it will create another file called “cargo.lock” that contains the build information, and you should see the same output as above one with rustc.
What does this “cargo.lock” do ?
cargo.lock — this helps to maintain the repeatable build, so that the build information does not change when it is shared with others in your team.
Also you might wonder how cargo compiles your main.rs file without mentioning it in the cargo run command.
You will find the below lines in your cargo.toml file
This has your default compilation target as main.rs file, that is how it is able to decide your compilation target
Let’s discuss about the details from the code snippet in main.rs
FUNCTIONS
Functions in rust are written using the “fn” keyword followed by the name of the function which is “main” in the above example and parentheses. As rust is a strictly typed language return type should be mentioned for a function if it returns any value else not required.
PRINTLN!
println! is used to print the given data to the standard output
Let’s continue in the next part about variables in rust