📜  rust hello world (1)

📅  最后修改于: 2023-12-03 15:34:46.445000             🧑  作者: Mango

Rust Hello World

Rust is a modern systems programming language that emphasizes safety, performance, and concurrency. In this tutorial, we will go through how to write a simple "Hello, World!" program in Rust.

Setting up Rust

Before we can write our first Rust program, we need to set up Rust on our computer. To do this, we can follow the instructions on the Rust installation page.

Creating a New Rust Project

Let's create a new Rust project by running the following command in our terminal:

cargo new hello_world

This command will create a new directory called hello_world with a default Rust project structure. Our main code will reside in the file src/main.rs.

Writing the Program

Now, let's open src/main.rs in our text editor and write our "Hello, World!" program:

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

This program defines a function called main, which is the entry point of a Rust program. Inside the function, we print the message "Hello, world!" to the console using the println! macro.

Running the Program

To run the program, we can use the following command:

cargo run

This command will compile our code and execute the resulting binary. We should see the following output in our terminal:

Hello, world!
Conclusion

In this tutorial, we learned how to write a simple "Hello, World!" program in Rust. We also learned how to set up a new Rust project and use Cargo to compile and run our code. Rust has many more features and capabilities, and we encourage you to explore them further by reading the Rust documentation. Happy coding!