Skip to content

Instantly share code, notes, and snippets.

@berkes
Created October 4, 2022 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkes/67d8d6dc1dc7a8d143682fa3cc61f9ec to your computer and use it in GitHub Desktop.
Save berkes/67d8d6dc1dc7a8d143682fa3cc61f9ec to your computer and use it in GitHub Desktop.
ViewCount With Sum
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