Rust中的模块及私有性控制

好像没有其它语言的private, protected关键字,应了一个public关键字。

mod plant {
    pub struct Vegetable {
	pub name: String,
	_id: i32,
    }

    impl Vegetable {
	pub fn new(name: &str) -> Vegetable {
	    Vegetable {
		name: String::from(name),
		_id: 1,
	    }
	}
    }
}

fn main() {
    let mut v = plant::Vegetable::new("squash");
    v.name = String::from("butternut squash");
    println!("{} are delicious", v.name);
}

  

猜你喜欢

转载自www.cnblogs.com/aguncn/p/11405174.html