Skip to content

Rust Organization and Management

Overview

Rust's module system helps you organize code.

📦 Module Definition

rust
mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

pub fn eat_at_restaurant() {
    front_of_house::hosting::add_to_waitlist();
}

Continue Learning: Next Chapter - Rust Generics and Traits

Rust Generics and Traits

Overview

Generics and traits are Rust's powerful abstraction tools.

🔧 Generic Functions

rust
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
    let mut largest = list[0];
    for &item in list {
        if item > largest {
            largest = item;
        }
    }
    largest
}

🎯 Trait Definition

rust
trait Summary {
    fn summarize(&self) -> String;
}

struct NewsArticle {
    pub headline: String,
    pub content: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("{}: {}", self.headline, self.content)
    }
}

Continue Learning: Next Chapter - Rust File and IO

Rust File and IO

Overview

Rust provides powerful file and IO operation capabilities.

📁 File Reading

rust
use std::fs;

fn main() {
    let contents = fs::read_to_string("hello.txt")
        .expect("Something went wrong reading the file");
    println!("With text:\n{}", contents);
}

📁 File Writing

rust
use std::fs;

fn main() {
    fs::write("hello.txt", "Hello, world!")
        .expect("Unable to write file");
}

Continue Learning: Next Chapter - Rust Collections and Strings

Rust Collections and Strings

Overview

Rust standard library contains many useful collection types.

📚 Vector

rust
fn main() {
    let mut v = Vec::new();
    v.push(5);
    v.push(6);
    v.push(7);
    v.push(8);

    let v = vec![1, 2, 3, 4, 5];
    let third: &i32 = &v[2];
    println!("The third element is {}", third);
}

📚 Strings

rust
fn main() {
    let mut s = String::new();
    let data = "initial contents";
    let s = data.to_string();
    let s = String::from("initial contents");

    let mut s = String::from("foo");
    s.push_str("bar");
    println!("{}", s);
}

📚 HashMap

rust
use std::collections::HashMap;

fn main() {
    let mut scores = HashMap::new();
    scores.insert(String::from("Blue"), 10);
    scores.insert(String::from("Yellow"), 50);

    let team_name = String::from("Blue");
    let score = scores.get(&team_name);
    println!("{:?}", score);
}

Continue Learning: Next Chapter - Rust Object-Oriented Programming

Rust Object-Oriented Programming

Overview

Rust is not a traditional object-oriented language, but supports many object-oriented features.

🏗️ Encapsulation

rust
pub struct AveragedCollection {
    list: Vec<i32>,
    average: f64,
}

impl AveragedCollection {
    pub fn add(&mut self, value: i32) {
        self.list.push(value);
        self.update_average();
    }

    pub fn remove(&mut self) -> Option<i32> {
        let result = self.list.pop();
        match result {
            Some(value) => {
                self.update_average();
                Some(value)
            }
            None => None,
        }
    }

    pub fn average(&self) -> f64 {
        self.average
    }

    fn update_average(&mut self) {
        let total: i32 = self.list.iter().sum();
        self.average = total as f64 / self.list.len() as f64;
    }
}

Continue Learning: Next Chapter - Rust Macros

Content is for learning and research only.