The Rust programming language : Hello World
This article will show you how to quickly get up and running with rust programming.
Installation
You’ll first need to install Visual C++ build tools. Download from here and run. When prompted to select workloads, it is recommended to select.NET desktop development, Desktop development with C++, and Universal Windows Platform development.
Next, simply download rustup-init.exe from here and run to install. Once installed, open a command prompt and type the following command to verify that the installation was successful.
>>> rustc --version
A successful installation will result in an output along the lines of
rustc 1.60.0 (7737e0b5c 2022-04-04)
Hello World
To create our first program, open up your text editor of choice and create a main.rs file. Create a function called main using the fn keyword
fn main(){}
To print out a statement, use the println macro (Note the exclamation mark!).
fn main(){
println!("Hello World!");}
And there we have our first program. To run this program, we’ll need to compile it first. In a command prompt in the same directory as main.rs, run the following command:
>>> rustc .\main.rs
This will generate a main.exe file in the same directory. To run the program, simply run:
>>>.\main1
and you should see your print statement!.
Using cargo
Cargo is a package manager for rust. It also provides us a single command to do both the compilation and running of a program.
To create a new program named new_program, run
>>> cargo new new_program
This should generate a new folder named new_program, which will contain an src directory with a main.rs file in it. You’ll notice that the file already has the code to print hello world in it. To run the program, move into the newly created directory in command prompt and run the cargo run command.
>>> cd new_program
>>> cargo run
Welcome to Rust!