-
-
Save rust-play/cedc3bb3d7a903aa37b2010428176bff to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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
| use chrono::{DateTime, Local, Duration}; | |
| use std::ops::Deref; | |
| struct Meeting { | |
| name: String, | |
| start_time: DateTime<Local>, | |
| } | |
| impl Deref for Meeting { | |
| type Target = DateTime<Local>; | |
| fn deref(&self) -> &Self::Target { | |
| &self.start_time | |
| } | |
| } | |
| fn main() { | |
| let project_kickoff = Meeting { | |
| name: String::from("Project Kickoff"), | |
| start_time: Local::now(), | |
| }; | |
| println!("--- Meeting Details ---"); | |
| println!("Meeting Name: {}", project_kickoff.name); | |
| println!("Start Time: {}", project_kickoff.format("%Y-%m-%d %H:%M:%S")); | |
| // Passes &Meeting, which successfully coerces into &DateTime<Local> | |
| print_days_until_deadline(&project_kickoff); | |
| } | |
| fn print_days_until_deadline(current_time: &DateTime<Local>) { | |
| // Add the asterisk '*' to dereference the pointer explicitly | |
| let deadline = *current_time + Duration::days(7); | |
| println!("Deadline: {}", deadline.format("%A, %B %d")); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=cedc3bb3d7a903aa37b2010428176bff