X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=concepts%2Fsrc%2Fmain.rs;h=4b6df6447be9930e5773202f24601cd817722eb8;hp=94bdb960aa7e79b80d4ad22f082ab2822e6c1fbc;hb=d6fbfabfaf434ae365854f0587547cff3ca78dae;hpb=89ba5f70f2de2cf038b2abb701a3d257c6b965d8;ds=sidebyside diff --git a/concepts/src/main.rs b/concepts/src/main.rs index 94bdb96..4b6df64 100644 --- a/concepts/src/main.rs +++ b/concepts/src/main.rs @@ -247,6 +247,42 @@ fn test_threads() { println!("counter: {}", *counter.lock().unwrap()); } +fn test_dyn_traits() { + let text = "I ate a salad for lunch today"; + let mut post = Post::new(); + post.add_text(text); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.reject(); + assert_eq!("", post.content()); + + post.request_review(); + assert_eq!("", post.content()); + + post.approve(); // first + assert_eq!("", post.content()); + + post.approve(); // second + assert_eq!(text, post.content()); + + post.reject(); // no-op + assert_eq!(text, post.content()); +} + +fn test_state_types() { + let mut post = TPost::new(); + post.add_text("I ate a salad for lunch"); + let post = post.request_review(); + let mut post = post.reject(); + post.add_text(" today"); + let post = post.request_review(); + let post = post.approve(); + assert_eq!(post.content(), "I ate a salad for lunch today"); +} + fn main() { test_strings(); test_vectors(); @@ -256,4 +292,6 @@ fn main() { test_closures(); test_iterators(); test_threads(); + test_dyn_traits(); + test_state_types(); }