Learning-Rust.Github.io: Rust Programming Language Tutorials for Everyone
Learning-Rust.Github.io: Comprehensive Rust Tutorials for All
Welcome to Learning Rust, a dedicated resource providing Rust Programming Language Tutorials for Everyone! Whether you are a seasoned developer or a complete novice, this platform is designed to help you master the language.
๐ Getting Started
Before diving into the code, ensure you have your environment ready:
- Install the Rust toolchain via
rustup. - Configure your favorite IDE (e.g., VS Code with rust-analyzer).
- Complete your first "Hello World" project.
"Rust empowers developers to write memory-safe code without a garbage collector, combining the speed of C++ with modern safety guarantees."
๐ ๏ธ Exploring the Syntax
Let's examine how Rust handles various programming paradigms through practical examples.
1. Data and Behavior: Structs, Impls, and Traits
In Rust, we separate the data (state) from the behavior (logic). We use struct for data, impl for methods, and trait for shared interfaces.
Example Implementation:
struct Person {
name: String,
company_name: String,
}
impl Person {
fn new(name: String, company_name: String) -> Self {
Self { name, company_name }
}
fn intro(&self) -> String {
format!("I'm {} from {}", self.name, self.company_name)
}
}
trait Greet {
const PREFIX: &'static str = "Hello";
fn greet(&self) -> String {
format!("{}!", Self::PREFIX)
}
}
impl Greet for Person {}
fn main() {
let steve = Person::new("Steve".to_string(), "Apple".to_string());
println!("{}", steve.intro()); // Output: I'm Steve from Apple
}
2. Functional Programming: Iterators, Filters, and Maps
Rust supports a powerful functional style. You can transform data streams using a pipeline of operations. Mathematically, the summation process in the code below can be represented as: where handles the conditional scaling of the input.
fn main() {
let marks = ["10", "20", "30", "0.4", "0.5", "invalid"];
let sum: i32 = marks
.iter()
.filter_map(|num| num.parse::<f64>().ok()) // Remove ~~invalid~~ entries
.map(|num| if num < 1.0 { num * 100.0 } else { num })
.fold(0.0, |acc, x| acc + x) as i32;
println!("Final: {sum}"); // Expected: 150
}
3. Advanced Blueprints: Enums, Generics, and HashMaps
For more complex architectures, Rust provides Enums (which can hold data) and Generics to create flexible, reusable code.
use std::collections::HashMap;
enum Data<K, V> {
Value(V),
KeyValue(K, V),
}
fn main() {
let data = vec![
Data::KeyValue("Steve", 10),
Data::Value(20),
Data::KeyValue("Bill", 30),
Data::Value(40),
];
let map = data.into_iter().fold(HashMap::new(), |mut map, item| {
match item {
Data::KeyValue(k, v) => { map.insert(k, v); },
Data::Value(v) => { map.insert("unknown", v); },
}
map
});
println!("Map: {:?}", map);
// Result: {"unknown": 40, "Bill": 30, "Steve": 10} (Note: 'unknown' is overwritten)
}
๐ค Contribution & Infrastructure
This project would not be possible without the incredible open-source tools used to build it.
| Tool | Purpose | Key Feature |
|---|---|---|
| GoHugo | Site Framework | The world's fastest framework for building websites. |
| Pagefind | Search Engine | Fully static search library optimized for large sites. |
| e25DX | Theme/Setup | Modern, modular technical documentation and blog layout. |
Interested in helping? Check out our contribution guidelines to help make Rust accessible to everyone!