Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from rust-play/playground.rs
Last active June 17, 2026 11:50
Show Gist options
  • Select an option

  • Save RandyMcMillan/dd3b448c5de31da38819fc0faf5dbb68 to your computer and use it in GitHub Desktop.

Select an option

Save RandyMcMillan/dd3b448c5de31da38819fc0faf5dbb68 to your computer and use it in GitHub Desktop.
chrono_coercion.rs
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"));
}
@RandyMcMillan

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment