Skip to main content

Command Palette

Search for a command to run...

Beyond Introduction: Hands-on Rust with Hello, World!

Updated
2 min read
Beyond Introduction: Hands-on Rust with Hello, World!
M

Hey there! I'm Fasil, an AI enthusiast and developer with a wide range of experience in various programming languages and frameworks. I have a strong background in Python, Node.js, Kotlin, and Spring Boot, where I've developed and deployed solutions for various applications.

My passion for AI extends beyond development—I'm constantly exploring new technologies and techniques to push the boundaries of what's possible in the field.

Welcome back to the exciting world of Rust! In the previous article, we explored the basics of Rust and set up our development environment. Now, it's time to dive into your first hands-on experience - writing and running a "Hello, World!" program.

Writing Your First Code:

  1. Create a new project: Open your terminal and navigate to your desired project directory. Run the following command to create a new Rust project named hello_world:
cargo new hello_world

This will create a new directory with the essential project structure, including a src folder where you'll write your code.

  1. Open the code file: Navigate to the src folder and open the file named main.rs. This is the entry point for your program.

  2. Write the code: Paste the following code into main.rs:

Rust

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

Let's break down what this code does:

  • fn main() - This line defines the main function, which is the starting point of your program.

  • {} - These curly braces mark the body of the function, where your program's logic resides.

  • println!("Hello, World!") - This line uses the println! macro to print the string "Hello, World!" to the console.

Compiling and Running:

  1. Save the code: Make sure you save the changes you made to the main.rs file.

  2. Compile the code: In your terminal, navigate back to the main project directory and run the following command:

cargo run

This will compile your main.rs file into an executable program. If everything is successful, you should see no errors in the output.

  1. Run the program: Now, run the following command:
./target/debug/hello_world

This will execute the compiled program and print the message "Hello, World!" to your console. Congratulations, you've successfully written and run your first Rust program!

Understanding the Process:

This simple example demonstrates the basic steps involved in Rust development:

  • Writing code: You use the main.rs file to write your program logic using Rust syntax.

  • Compiling: The cargo run command compiles your code into an executable program.

  • Running: You execute the compiled program to see its output.

Next Steps:

In the next article, we'll delve deeper into Rust's fundamental concepts like variables, data types, and control flow. We'll build upon this foundation to write more complex and interactive programs. Stay tuned for exciting adventures in the world of Rust!

More from this blog

Untitled Publication

25 posts