src/bin/lesson00_rust.rs

教程导航 · 代码索引

src/bin/lesson00_rust.rs

教程导航 · 代码索引

原文件:Doc/tutorial/src/bin/lesson00_rust.rs。

阅读快照 以下内容来自 2026-11-17 导入时的本地源码,后续不会自动同步。编辑、编译和运行请回到原项目;本页 Markdown 不是可直接编译的源文件。

在原项目根目录运行:

cargo run --manifest-path Doc/tutorial/Cargo.toml --bin lesson00_rust
struct Counter {
    value: u32,
}

fn increment(counter: &mut Counter) {
    counter.value += 1;
}

trait Describe {
    fn description(&self) -> String;
}

impl Describe for Counter {
    fn description(&self) -> String {
        format!("Count: {}", self.value)
    }
}

fn main() {
    let mut counter = Counter { value: 0 };
    increment(&mut counter);
    let add_two = |value: u32| value + 2;
    counter.value = add_two(counter.value);
    println!("{}", counter.description());
    let mut names = vec![String::from("Alice"), String::from("Bob")];
    names.retain(|name| name != "Alice");
    println!("Remaining: {}", names[0]);
}