Skip to main content

Crate srs_sm2

Crate srs_sm2 

Source
Expand description

Pure implementation of the SuperMemo SM-2 spaced-repetition algorithm.

srs-sm2 is a tiny, lightweight, #![no_std]-compatible library that computes when a flashcard should be reviewed next. Given a card’s current schedule and the quality of a recall, it returns the next schedule — with no I/O, no allocations beyond what the caller supplies, and no clock access. That makes it equally at home in a browser WASM bundle, an embedded device, or a server.

§The algorithm

SM-2 tracks two numbers per card:

  • interval_days — days until the next review.
  • ease_factor — a multiplier (>= MIN_EASE_FACTOR) controlling how fast the interval grows.

On each review the caller supplies a quality in 0..=5:

  • quality < 3 is a failed recall: the interval resets to 1 day (the card is due again tomorrow) but the ease factor is still updated.
  • The first successful review seeds the interval to 1 day, the second to 6 days, and every review after that multiplies the previous interval by the (updated) ease factor.

The ease factor is updated every review with the classic SM-2 formula:

EF' = EF + (0.1 - (5 - q) * (0.08 + (5 - q) * 0.02))

clamped to never drop below MIN_EASE_FACTOR (1.3).

§Examples

use srs_sm2::{Schedule, schedule_next, MIN_EASE_FACTOR};

// A brand-new card, never reviewed.
let card = Schedule { interval_days: 0, ease_factor: 2.5 };

// Perfect recall (quality 5) → first interval is seeded to 1 day.
let next = schedule_next(card, 5);
assert_eq!(next.interval_days, 1);

// A second good review seeds 6 days.
let next = schedule_next(next, 4);
assert_eq!(next.interval_days, 6);

// A continued perfect streak grows the interval by the ease factor.
let next = schedule_next(next, 5);
assert_eq!(next.interval_days, 16); // 6 * 2.6, rounded
assert!(next.ease_factor >= MIN_EASE_FACTOR);

A failed recall (quality < 3) resets the interval without dropping the ease factor below the floor:

use srs_sm2::{Schedule, schedule_next, MIN_EASE_FACTOR};

let card = Schedule { interval_days: 30, ease_factor: 2.5 };
let failed = schedule_next(card, 2);
assert_eq!(failed.interval_days, 1);
assert!(failed.ease_factor >= MIN_EASE_FACTOR);

Structs§

Schedule
A card’s schedule state, independent of storage or rendering.

Constants§

MIN_EASE_FACTOR
The lowest an ease factor may fall. Below this, intervals collapse toward daily review; SM-2 fixes the floor at 1.3.

Functions§

schedule_next
Compute the next Schedule from the current one and a recall quality in 0..=5.