DEV Community

Ahmed Gouda
Ahmed Gouda

Posted on • Originally published at ahmedgouda.hashnode.dev

The Rust Programming Language

The Rust Programming Language

The Rust programming language helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option to control low-level details (such as memory usage) without all the hassle traditionally associated with such control.

Hello, World!

Creating a Project Directory

Open a terminal and enter the following commands to make a projects directory and a directory for the “Hello, world!” project within the projects directory.

> mkdir ~/projects
> cd ~/projects
> mkdir hello_world
> cd hello_world
Enter fullscreen mode Exit fullscreen mode

Writing and Running a Rust Program

Next, make a new source file and call it main.rs. Rust files always end with the .rs extension. If you’re using more than one word in your filename, use an underscore to separate them. For example, use hello_world.rs rather than helloworld.rs.

> New-Item main.rs
Enter fullscreen mode Exit fullscreen mode

Now open the main.rs file you just created and enter the code below:

fn main() {
    println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode

Save the file and go back to your terminal window. Enter the following commands to compile and run the file:

> rustc main.rs
> .\main.exe
Hello, world!
Enter fullscreen mode Exit fullscreen mode

Anatomy of a Rust Program

Let’s review in detail what just happened in the “Hello, world!” program. Here’s the first piece of the puzzle:

fn main() {

}
Enter fullscreen mode Exit fullscreen mode

These lines define a function in Rust. The main function is special: it is always the first code that runs in every executable Rust program. The first line declares a function named main that has no parameters and returns nothing. If there were parameters, they would go inside the parentheses, ().

Also, note that the function body is wrapped in curly brackets, {}. Rust requires these around all function bodies. It’s good style to place the opening curly bracket on the same line as the function declaration, adding one space in between.

Inside the main function is the following code:

    println!("Hello, world!");
Enter fullscreen mode Exit fullscreen mode

This line does all the work in this little program: it prints text to the screen. There are four important details to notice here.

First, Rust style is to indent with four spaces, not a tab.

Second, println! calls a Rust macro. If it called a function instead, it would be entered as println (without the !). We’ll discuss Rust macros later. For now, you just need to know that using a ! means that you’re calling a macro instead of a normal function, and that macros don’t always follow the same rules as functions.

Third, you see the "Hello, world!" string. We pass this string as an argument to println!, and the string is printed to the screen.

Fourth, we end the line with a semicolon (;), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.

Compiling and Running Are Separate Steps

Before running a Rust program, you must compile it using the Rust compiler by entering the rustc command and passing it the name of your source file:

> rustc main.rs
Enter fullscreen mode Exit fullscreen mode

If you have a C or C++ background, you’ll notice that this is similar to gcc or clang. After compiling successfully, Rust outputs a binary executable.

If you’re more familiar with a dynamic language, such as Ruby, Python, or JavaScript, you might not be used to compiling and running a program as separate steps. Rust is an ahead-of-time compiled language, meaning you can compile a program and give the executable to someone else, and they can run it even without having Rust installed. If you give someone a .rb, .py, or .js file, they need to have a Ruby, Python, or JavaScript implementation installed (respectively). But in those languages, you only need one command to compile and run your program. Everything is a trade-off in language design.

Just compiling with rustc is fine for simple programs, but as your project grows, you’ll want to manage all the options and make it easy to share your code. Thus, we’ll use the Cargo tool, which will help you write real-world Rust programs.

Top comments (0)