(相关资料图)
pub struct AveragedCollection { list: Vec, average: f64,}impl AveragedCollection { pub fn add(&mut self, value: i32) { self.list.push(value); self.update_average(); } pub fn remove(&mut self) -> Option { 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; }}
Trait 动态 lib.rs 文件
pub trait Draw { fn draw(&self);}pub struct Screen { pub components: Vec>,}impl Screen { pub fn run(&self) { for component in self.components.iter() { component.draw(); } }}pub struct Button { pub width: u32, pub height: u32, pub label: String,}impl Draw for Button { fn draw(&self) { // 绘制一个按钮 }}
泛型的实现 一次只能实现一个类型
pub struct Screen { pub components: Vec,}impl ScreenwhereT: Draw,{ pub fn run(&self) { for component in self.components.iter() { component.draw() } }}
main.rs 文件
use oo::Draw;use oo::{Button, Screen};struct SelectBox { width: u32, height: u32, options: Vec,}impl Draw for SelectBox { fn draw(&self) { // 绘制一个选择框 }}fn main() { let screen = Screen { components: vec![ Box::new(SelectBox { width: 75, height: 10, options: vec![ String::from("Yes"), String::from("Maybe"), String::from("No"), ], }), Box::new(Button { width: 50, height: 10, label: String::from("OK"), }), ], }; screen.run();}
lib.rs 文件
pub trait Draw { fn draw(&self);}pub trait Clone { fn clone(&self) -> Self;}pub struct Screen { pub components: Vec>, // 报错}
例子:发布博客的工作流程 main.rs
use blog::Post;fn main() { let mut post = Post::new(); post.add_text("I ate a salad for lunch today"); assert_eq!("", post.content()); post.request_review(); assert_eq!("", post.content()); post.approve(); assert_eq!("I ate a salad for lunch today", post.content());}
lib.rs 文件
pub struct Post { state: Option>, content: String,}impl Post { pub fn new() -> Post { Post { state: Some(Box::new(Draft {})), content: String::new(), } } pub fn add_text(&mut self, text: &str) { self.content.push_str(text); } pub fn content(&self) -> &str { "" } pub fn request_review(&mut self) { if let Some(s) = self.state.take() { self.state = Some(s.request_review()) } } pub fn approve(&mut self) { if let Some(s) = self.state.take() { self.state = Some(s.approve()) } }}trait State { fn request_review(self: Box) -> Box; fn approve(self: Box) -> Box;}struct Draft {}impl State for Draft { fn request_review(self: Box) -> Box { Box::new(PendingReview {}) } fn approve(self: Box) -> Box { self }}struct PendingReview {}impl State for PendingRevew { fn request_review(self: Box) -> Box { self } fn approve(self: Box) -> Box { Box::new(Published {}) }}struct Published {}impl State for Published { fn request_review(self: Box) -> Box { self } fn approve(self: Box) -> Box { self }}
修改之后:
pub struct Post { state: Option>, content: String,}impl Post { pub fn new() -> Post { Post { state: Some(Box::new(Draft {})), content: String::new(), } } pub fn add_text(&mut self, text: &str) { self.content.push_str(text); } pub fn content(&self) -> &str { self.state.as_ref().unwrap().content(&self) } pub fn request_review(&mut self) { if let Some(s) = self.state.take() { self.state = Some(s.request_review()) } } pub fn approve(&mut self) { if let Some(s) = self.state.take() { self.state = Some(s.approve()) } }}trait State { fn request_review(self: Box) -> Box; fn approve(self: Box) -> Box; fn content<"a>(&self, post: &"a Post) -> &"a str { "" }}struct Draft {}impl State for Draft { fn request_review(self: Box) -> Box { Box::new(PendingReview {}) } fn approve(self: Box) -> Box { self }}struct PendingReview {}impl State for PendingRevew { fn request_review(self: Box) -> Box { self } fn approve(self: Box) -> Box { Box::new(Published {}) }}struct Published {}impl State for Published { fn request_review(self: Box) -> Box { self } fn approve(self: Box) -> Box { self } fn content<"a>(&self, post: &"a Post) -> &"a str { &post.content }}
lib.rs 代码:
pub struct Post { content: String,}pub struct DraftPost { content: String,}impl Post { pub fn new() -> DraftPost { DraftPost { content: String::new(), } } pub fn content(&self) -> &str { &self.content }}impl DraftPost { pub fn add_text(&mut self, text: &str) { self.content.push_str(text); } pub fn request_review(self) -> PendingReviewPost { PendingReviewPost { content: self.content, } }}pub struct PendingReviewPost { content: String,}impl PendingReviewPost { pub fn approve(self) -> Post { Post { content: self.content, } }}
main.rs 代码:
use blog::Post;fn main() { let mut post = Post::new(); post.add_text("I ate a salad for lunch today"); let post = post.request_review(); let post = post.approve(); assert_eq!("I ate a salad for lunch today", post.content());}
X 关闭
Copyright © 2015-2022 大众安防网版权所有 备案号:豫ICP备20014643号-14 联系邮箱: 905 14 41 07@qq.com