Lädt...


🔧 Writing your first program in Rust


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Introduction

Writing your first program in Rust is a great beginning to unlocking a lot of goodies that Rust has to offer.

In this tutorial we will write our first Rust program, and also dive into the rust package manager which is Cargo, we shall learn how to use Cargo and make life easier as a Rust developer.

Creating a project directory

To get started with your program, create a project directory in your Mac, or Linux OS following steps.

mkdir newproject

Change directory into the newly created directory with the code snippet below.

cd newproject

Writing a new Rust project

It has become a general standard to start writing Rusts program with a main.rs file, and rust files have an extension that ends with .rs . That way, our program will follow the same protocol by creating a main.rs file in our project directory.

Open main.rs and write a main function that prints Hello, world! as seen below.

fn main() {

println!("Hello, world");

}

Save your project and run it in your terminal by typing main.rs.

If the program compiles and runs correctly, Hello, world! will be printed in your terminal.

Now, what’s happening in the program we just wrote?

The first part of the program which is the function keyword which is denoted as fn serves as the function definition that serves as the entry point our project and to all Rusts’ programs, this is the first function that runs in every Rust program; therefore it is important to declare a main function that will compile and run without issues.

The other part is the println!("Hello, world"); line that is compiled by the function and prints the line on our terminal, this is called the body of the function. To explain further the println! keyword calls a Rust built-in in function called macro .

What is Cargo?

To make writing programs in Rust even easier, a package manager and build system for Rust was developed. It's called Cargo, there are a lot of libraries in the Rust ecosystem that are working together to deliver the best programming experience to Rustaceans, cargo therefore handles the tasks of downloading the libraries, and it is also used to build the libraries and make them work in your programs.

As you write more complex programs, you will need to use more libraries in the Rust’s ecosystem, therefore you need the service of the package manager, therefore, installing rust with the rustup installation will automatically install Cargo on your local machine.

To confirm if you have Cargo installed on your computer, use cargo —version .

How to create a new project with Cargo.

Getting started with Cargo is as simple as getting started with your Pizza, run the code snippets below and boom, you have a new Cargo project.

 cargo new new_cargo_project
 cd new_cargo_project

To confirm if you have a new Cargo project, check your project’s directory and you should find the new_cargo_project directory which automatically has a src file in that contains a main.rs file.

As per normal standard, lets create a hello world program to test run Cargo.

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

Let us build and run the Cargo project in our terminal using the code snippet below.

cargo build

If all goes well, this command builds and runs the cargo project and prints Hello world in our terminal. Once a build is successful, you can always use cargo run to run every new update that you have in your project.

And that is it, we have successfully written a hello world program in Rust.

Conclusion

In this tutorial, we have been introduced to the very beginning of every programing language which is to write a successful hello world function, we also learnt the standard way of working with Rust package manager which cargo and using it to build and run our Rust programs.

Going forward, we shall begin to learn more about the fundamental principles of the Rust programming language and subsequently build more complex projects.

...

🔧 Writing your first program in Rust


📈 33.79 Punkte
🔧 Programmierung

🔧 Writing your first program in Rust


📈 33.79 Punkte
🔧 Programmierung

📰 Rust Basics Series #1: Create and Run Your First Rust Program


📈 31.6 Punkte
🐧 Unix Server

🔧 Writing Visual Studio Extensions with Mads - Writing your first extension


📈 28.64 Punkte
🔧 Programmierung

🔧 Writing a Multithreaded Image Dithering Program with Rust 🦀🖼️


📈 25.79 Punkte
🔧 Programmierung

🔧 Quantum Computing Basics: Writing Your First Quantum Program ⚛️💻


📈 25.65 Punkte
🔧 Programmierung

🔧 Writing Your First JavaScript Program.


📈 25.65 Punkte
🔧 Programmierung

🔧 How to learn Go: Setting up Go and writing your first program


📈 25.65 Punkte
🔧 Programmierung

📰 Rust Basics Series #8: Write the Milestone Rust Program


📈 23.61 Punkte
🐧 Unix Server

📰 10 Best AI Writing Tools (2024): Enhance Your Writing with AI Magic


📈 23.46 Punkte
📰 IT Nachrichten

🔧 5 Content Writing Tools to Improve your Content Writing Skills


📈 23.46 Punkte
🔧 Programmierung

📰 Writing Cybersecurity Articles – Setting Up Your Writing Process


📈 23.46 Punkte
📰 IT Security Nachrichten

🔧 5. Writing our first program in python with home🧡work


📈 22.84 Punkte
🔧 Programmierung

🔧 Your Guide to Better Documentation: Writing Your First Draft and Beyond


📈 21.13 Punkte
🔧 Programmierung

🐧 My journey with Linux: from a total noob to building and publishing my first Rust program


📈 20.65 Punkte
🐧 Linux Tipps

🔧 Started Out Writing About Algorithms - Somehow Ended Up Writing Something Totally Different


📈 20.64 Punkte
🔧 Programmierung

🐧 Comparing the writing speeds of ISO writing tools


📈 20.64 Punkte
🐧 Linux Tipps

🎥 Can an AI writing assistant match my personal writing style?


📈 20.64 Punkte
🎥 Videos

🔧 What is Content Writing? 10 Best Tips for Great Content Writing


📈 20.64 Punkte
🔧 Programmierung

🔧 Writing Visual Studio Extensions with Mads - Writing JSON Schemas


📈 20.64 Punkte
🔧 Programmierung

🔧 My first program that made me money. The destroy program


📈 19.85 Punkte
🔧 Programmierung

🔧 "Mastering Rust: Your Ultimate Rust Cheat Sheet for Safe and Fast Programming"


📈 19.09 Punkte
🔧 Programmierung

🎥 RustZone: Writing Trusted Applications in Rust


📈 18.46 Punkte
🎥 IT Security Video

📰 Writing LLMs in Rust: Looking for an Efficient Matrix Multiplication


📈 18.46 Punkte
🔧 AI Nachrichten

🎥 RustZone: Writing Trusted Applications in Rust


📈 18.46 Punkte
🎥 IT Security Video

🔧 5 Tips for Writing Clean, Idiomatic Rust Code 🦀✨


📈 18.46 Punkte
🔧 Programmierung

🐧 Writing Linux Kernel Modules in Safe Rust | Linux Security Summit 2019


📈 18.46 Punkte
🐧 Linux Tipps

🔧 Writing Rust Documentation


📈 18.46 Punkte
🔧 Programmierung

🐧 Writing Linux Kernel Module in Rust


📈 18.46 Punkte
🐧 Linux Tipps

🐧 bign-handheld-thumbnailer: What I learned from writing a Linux thumbnailer in Rust | MateusRodCosta


📈 18.46 Punkte
🐧 Linux Tipps

🔧 Writing a WASM module in Rust


📈 18.46 Punkte
🔧 Programmierung

🐧 Righi: Writing a scheduler for Linux in Rust that runs in user-space


📈 18.46 Punkte
🐧 Linux Tipps

matomo