【Rust】生存期方法

环境

  • Rust 1.56.1
  • VSCode 1.61.2

概念

参考:https://doc.rust-lang.org/stable/rust-by-example/scope/lifetime/methods.html

示例

main.rs

struct Owner(i32);

impl Owner {
    fn add_one<'a>(&'a mut self) {
        self.0 += 1;
    }
    fn print<'a>(&'a self) {
        println!("`print`: {}", self.0);
    }
}

fn main() {
    let mut owner = Owner(18);

    owner.add_one();
    owner.print();
}

总结

了解了 Rust 中的方法的生存期参数的标注。

附录

原文地址:https://www.cnblogs.com/jiangbo44/p/15703797.html