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

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:
- 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.
Open the code file: Navigate to the
srcfolder and open the file namedmain.rs. This is the entry point for your program.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 themainfunction, 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 theprintln!macro to print the string "Hello, World!" to the console.
Compiling and Running:
Save the code: Make sure you save the changes you made to the
main.rsfile.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.
- 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.rsfile to write your program logic using Rust syntax.Compiling: The
cargo runcommand 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!




