🔧 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
🔧 Writing Your First JavaScript Program.
📈 25.65 Punkte
🔧 Programmierung
🐧 Comparing the writing speeds of ISO writing tools
📈 20.64 Punkte
🐧 Linux Tipps
🔧 Writing Rust Documentation
📈 18.46 Punkte
🔧 Programmierung
🐧 Writing Linux Kernel Module in Rust
📈 18.46 Punkte
🐧 Linux Tipps
🔧 Writing a WASM module in Rust
📈 18.46 Punkte
🔧 Programmierung