Hello World In Rust ( Rust For Beginners ) with source code
In this article, we learn how to print Hello World In Rust Programming language.
Firstly we need to make sure that rust Is installed. to check If rust Is installed or not you just have to run the command
rustup — version
If you don't install rust yet, you can follow this article:
Rust For Beginners ( installation )
If you are using VS Code You should Install the extension Rust
Once you have done,
we start with an empty folder, there are two ways to do it and I’m going to show you both of them.
So, we start without cargo ( rust package manager ) I open a terminal In an empty folder named “Hello World In Rust” and create a file with the command:
touch hello.rs
( touch Is used to create a file, you can create the file in the normal way. I like to do more work with terminal commands). and open that up ( you can open It by simply clicking on it and you can use command.
Code hello.rs
just replace hello.rs with your file.)
So In this file, we create our main function
Fn main
fn is the keyword used to initialize a function ( just like In js we use keyword function main In rust, we used fn in the replacement of function ).
In rust, we use the command println! to see the output.
If we want to compile this and run it we can simply use
rustc hello.rs
Now you can see it create some files to run the file from the command line
you have to type “./hello” and It will print “Hello World” (Or whatever you write In it )
But this is not how you work In the real world. So now, we just delete all the files from the folder. there Are a couple of ways to start your rust project with cargo.
(Just make sure your folder name doesn’t contain space) We use cargo init command. now here we have “Cargo.toml” file. This file has all detail of our package
and a .gitignore file which has “‘/target”, what Is basically do when we run compiler all the files so In the target folder and this is the thing you don’t have to push on GitHub. In our src folder here we do all the rust coding and you can see there Is a
main.rs file that has the same code we just did a couple Of mints ago.
Now, to compile this we use the command
cargo run
what does It do, it gonna compile it and run it in the terminal
Hello, world!
Here Is the link to all the commands we learn so far. and here Is the git repo link, this is the first commit without cargo and with cargo.