Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

rust
hello world

Making a "Hello World" program in Rust

Educative Answers Team

The journey to learning any programming language starts with a simple “Hello World” program. To create a “Hello World” program in Rust, follow these steps:

  1. Create a source file and name it main.rs – rust files always end with the .rs extension.
  2. Open the main.rs file and write the following code in it:
fn main() {
  println!("Hello world!");
} 
  1. Save the file and open a terminal window in the directory you are working in.
  2. If you are using Linux or MacOS, enter the following commands to execute the program:
$ rustc main.rs
$ ./main
  1. If you are using Windows, then the commands to compile and execute the program are:
> rustc main.rs
> .\main.exe

The following is an executable “Hello World” program in Rust​:

fn main() {
  println!("Hello World!");
}
svg viewer

Explanation

  • The code fn main() {} defines a function named main. The main function is special because it is always the first code that runs in a Rust executable program. Any arguments will go inside the parenthesis () and the code will go inside the curly brackets {}.

  • The code println!("Hello, world!"); simply prints the string inside the parenthesis onto the screen. Note that println! has an exclamation mark at the end of it – ​this means that it is a macro and not a function. While functions generate a function call, macros are expanded into a source code that is compiled with the rest of the program.

RELATED TAGS

rust
hello world
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring