rust
38 lines · 7 steps
Parsing and iterating date ranges in Rust
A DateRange type that validates a "start/end" string and lazily yields every day it covers.
Explained by
highlit
1use chrono::{Duration, NaiveDate};
2
3#[derive(Debug)]
4pub struct DateRange {
5 start: NaiveDate,
6 end: NaiveDate,
7}
8
9impl DateRange {
10 pub fn parse(spec: &str) -> Result<Self, String> {
11 let (start_raw, end_raw) = spec
12 .split_once('/')
13 .ok_or_else(|| format!("missing '/' separator in range: {spec}"))?;
14
15 let start = NaiveDate::parse_from_str(start_raw.trim(), "%Y-%m-%d")
16 .map_err(|e| format!("invalid start date {start_raw:?}: {e}"))?;
17 let end = NaiveDate::parse_from_str(end_raw.trim(), "%Y-%m-%d")
18 .map_err(|e| format!("invalid end date {end_raw:?}: {e}"))?;
19
20 if end < start {
21 return Err(format!("end {end} precedes start {start}"));
22 }
23
24 Ok(Self { start, end })
25 }
26
27 pub fn days(&self) -> impl Iterator<Item = NaiveDate> {
28 let end = self.end;
29 std::iter::successors(Some(self.start), move |¤t| {
30 let next = current + Duration::days(1);
31 (next <= end).then_some(next)
32 })
33 }
34
35 pub fn len(&self) -> i64 {
36 (self.end - self.start).num_days() + 1
37 }
38}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1The `?` operator lets a parser bail early while attaching a descriptive error at each fallible step.
- 2`std::iter::successors` turns a seed and a step function into a lazy, allocation-free sequence.
- 3Capturing owned values with `move` frees an iterator from borrowing the struct that produced it.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/parsing-and-iterating-date-ranges-in-rust-explained-rust-65b9/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.