diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-14 13:17:23 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-14 13:17:23 -0600 |
| commit | 495283b1c3cb233749fab69a8e41e6e41988bb2b (patch) | |
| tree | 5c1e715343206425541423caa9bc18abeac5a283 | |
| parent | afa39bf9f5a25cbd49998d37fd224063c3894332 (diff) | |
feat: learn_8
commit 2fa3dba9a2444ba9dfdc8c35a37ef4713e337d83
Author: Brad Clawsie <brad@b7j0c.org>
Date: Sat Nov 14 13:17:07 2026 -0600
feat: learn_8
| -rw-r--r-- | learn_8/Cargo.lock | 7 | ||||
| -rw-r--r-- | learn_8/Cargo.toml | 6 | ||||
| -rw-r--r-- | learn_8/src/main.rs | 108 |
3 files changed, 121 insertions, 0 deletions
diff --git a/learn_8/Cargo.lock b/learn_8/Cargo.lock new file mode 100644 index 0000000..39dea36 --- /dev/null +++ b/learn_8/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "learn_8" +version = "0.1.0" diff --git a/learn_8/Cargo.toml b/learn_8/Cargo.toml new file mode 100644 index 0000000..6cee840 --- /dev/null +++ b/learn_8/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "learn_8" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/learn_8/src/main.rs b/learn_8/src/main.rs new file mode 100644 index 0000000..b1de902 --- /dev/null +++ b/learn_8/src/main.rs @@ -0,0 +1,108 @@ +struct Document { + title: String, + body: String, +} + +struct DocumentView<'a> { + title: &'a str, + body: &'a str, +} + +impl Document { + pub fn new(title: String, body: String) -> Self { + Self { + title, + body, + } + } + + pub fn view(&self) -> DocumentView<'_> { + DocumentView{ + title: &self.title, + body: &self.body, + } + } +} + +impl<'a> DocumentView<'a> { + pub fn title(&self) -> &str { + self.title + } + + pub fn body(&self) -> &str { + self.body + } + + pub fn word_count(&self) -> usize { + self.body.split_whitespace().count() + } + + pub fn summary(&self, n: usize) -> &str { + match self.body.char_indices().nth(n) { + Some((idx, _)) => &self.body[..idx], + None => &self.body, + } + } +} + +fn main() { + () +} + +#[cfg(test)] +mod tests { + use crate::*; + + #[test] + fn test_document_new() { + let document = Document::new("title".to_string(), "body".to_string()); + assert_eq!(document.title, "title".to_string()); + assert_eq!(document.body, "body".to_string()); + } + + #[test] + fn test_document_view() { + let document = Document::new("title".to_string(), "body".to_string()); + let view = document.view(); + assert_eq!(view.title, "title"); + assert_eq!(view.body, "body"); + } + + #[test] + fn test_document_view_title() { + let document = Document::new("title".to_string(), "body".to_string()); + let view = document.view(); + assert_eq!(view.title(), "title"); + } + + #[test] + fn test_document_view_body() { + let document = Document::new("title".to_string(), "body".to_string()); + let view = document.view(); + assert_eq!(view.body(), "body"); + } + + #[test] + fn test_document_view_word_count() { + let empty = Document::new("title".to_string(), "".to_string()); + let empty_view = empty.view(); + assert_eq!(empty_view.word_count(), 0); + let document = Document::new("title".to_string(), "body body body".to_string()); + let view = document.view(); + assert_eq!(view.word_count(), 3); + } + + #[test] + fn test_document_view_summary() { + let document0 = Document::new("title".to_string(), "hello world".to_string()); + let view0 = document0.view(); + assert_eq!(view0.summary(5), "hello"); + let document1 = Document::new("title".to_string(), "café ☕".to_string()); + let view1 = document1.view(); + assert_eq!(view1.summary(4), "café"); + let document2 = Document::new("title".to_string(), "café ☕".to_string()); + let view2 = document2.view(); + assert_eq!(view1.summary(99), "café ☕"); + } + +} |
