1. Hello World
by
gg582 · 2026-05-23 10:48:08 · 36 views
Introduction to Cargo
First, I am deeply into the C language. In my opinion, rewrite and conquer is a stupid way, as the C language has improved in a few decades. As a result, I am learning Rust as a whole different language.
Also, I don't have any interest in rewriting something in Rust (some call it RIIR).
Cargo: The Modern App Creator
Rust uses cargo to create Rust apps.
Simply run this:
cargo run hello_world
Directory Structure
yjlee@yjlee-linuxonmac:~/learn-rust/hello_rust$ tree
.
├── Cargo.toml
└── src
└── main.rs
2 directories, 2 files
yjlee@yjlee-linuxonmac:~/learn-rust/hello_rust$
The project file is called Cargo.toml.
Let's read this post.
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2024"
[dependencies]
Rust has various editions, like Rust 2021 and Rust 2024. Many Rust communities recommend Rust 2024, as it supports more syntactic sugar. And people say that it is safer, but I am not sure why programming languages always try to do everything instead of programmers.
I am pretty sure I would compare C23 and Rust 2024 in my posts, but please consider I have a strong perspective based on C17/C23 conventions.
Main Function
The main function is still simple.
When you run main.rs, there are a few keywords that are notable.
fn main() {
println!("Hello, world!");
}
| Keyword | Meaning |
|---|---|
| fn | Function |
| main | Entrypoint |
| println | Macro Name |
| ! | Macro Func |
| { | Open Brace |
| } | Close Brace |
| "" | Literal Type |
| () | Retrieves Args |
| ; | Semicolon |
Build
cargo build
or directly run:
cargo run
Run
The default build is a debug build. Run this:
Unix / Linux
./target/debug/hello_rust
Windows
.\target\debug\hello_rust
Scope
Rust has a scope. And when a variable is asserted within a scope, you cannot* use it outside.
A scope is separated by {} braces.
fn main() {
// Let's call it "Scope 1"
{
let str1 = String::from("Hello, world!");
println!("{}", str1);
// str1 can be used in this scope
}
println!("{}", str1); // No parasan! You can't go inside.
}
You cannot compile this as you tried to use str1 outside of Scope 1.
This is also similar when you use Modern C++. It is definitely safe, but I'm still not sure if a programming language must do these things without a programmer's permission.
Next Steps?
- Int Types
- Float Types
Then...
- String Types
- If-Else If-Else