Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 20, 2022 11:31 pm GMT

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

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 youre 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

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

fn main() {    println!("Hello, world!");}

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

> rustc main.rs> .\main.exeHello, world!

Anatomy of a Rust Program

Lets review in detail what just happened in the Hello, world! program. Heres the first piece of the puzzle:

fn main() {}

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. Its 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!");

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 !). Well discuss Rust macros later. For now, you just need to know that using a ! means that youre calling a macro instead of a normal function, and that macros dont 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

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

If youre 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, youll want to manage all the options and make it easy to share your code. Thus, well use the Cargo tool, which will help you write real-world Rust programs.


Original Link: https://dev.to/ahmedgouda/the-rust-programming-language-18b0

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To