1. Installing Rust:
- Visit official Rust website for installation instructions.
- Open a terminal and run the installer.
2. Setting Up Your First Project:
- Create a new project:
cargo new my_first_rust_project
. - Navigate into the project:
cd my_first_rust_project
.
3. Cargo Build System:
- Use
cargo build
to compile your project. - Run your project:
cargo run
. - Manage dependencies in
Cargo.toml
.
4. Writing Your First Program:
- Open
src/main.rs
and replace with:
fn main() {
println!("Hello, Rust!");
}
5. Variables and Data Types:
- Declare variables with explicit types.
fn main() {
let my_integer: i32 = 42;
let my_float: f64 = 3.14;
let is_true: bool = true;
let my_char: char = 'A';
let my_string: &str = "Hello, Rust!";
}
6. Functions:
- Define functions using
fn
. - Example:
fn add_numbers(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let result = add_numbers(5, 7);
println!("The sum is: {}", result);
}
7. Ownership and Borrowing:
- Rust's ownership system ensures memory safety.
- Use borrowing for references without sacrificing performance.
Conclusion:
- Rust offers a modern, safe, and performant programming experience.
- Explore advanced features and community resources to deepen your Rust expertise. Happy coding!
Top comments (0)