Created
October 4, 2022 09:56
-
-
Save berkes/67d8d6dc1dc7a8d143682fa3cc61f9ec to your computer and use it in GitHub Desktop.
ViewCount With Sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod domain { | |
use std::ops::Deref; | |
pub struct ViewCount { value: usize } | |
impl ViewCount { | |
pub fn new(value: usize) -> Self { | |
Self { value } | |
} | |
} | |
impl Deref for ViewCount { | |
type Target = usize; | |
fn deref(&self) -> &Self::Target { | |
&self.value | |
} | |
} | |
pub struct Title { value: String, } | |
impl Title { | |
pub fn new(value: String) -> Self { Self { value }} | |
} | |
impl Deref for Title { | |
type Target = String; | |
fn deref(&self) -> &Self::Target { | |
&self.value | |
} | |
} | |
} | |
use domain::*; | |
fn main() { | |
let viewcounts = vec![ViewCount::new(3), ViewCount::new(5), ViewCount::new(7)]; | |
let sum: usize = Iterator::sum(viewcounts.into_iter().map(|vc| *vc)); | |
let report_title = Title::new("View Counts".to_string()); | |
let report_title_length = report_title.len(); | |
println!("-- {} ({})--", *report_title, report_title_length); | |
println!("total views: {}", sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment