expression

REST: keyset pagination using a stable key

Keyset pagination resumes after a position in an ordered collection rather than skipping N rows. This toy example uses unique increasing IDs. A production continuation token should be opaque and associated with the relevant query and ordering; never use it as proof of authorization. Concurrent changes still affect consistency unless snapshot semantics are provided. Standalone synthetic example. Expected stdout (not executed): [30, 40] last: 40 Common mistake: Treating the visible demonstration ID as a production opaque cursor or a snapshot guarantee.

Formal Syntax & Structure

rust syntaxrust
filter(|id| id > last_id).take(limit)

Working Example

Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    let ids = [10, 20, 30, 40, 50];    let after = 20;    let limit = 2;    let page: Vec<_> = ids.iter().copied().filter(|&id| id > after).take(limit).collect();    println!("{page:?}");    println!("last: {}", page.last().unwrap());}
Rust Reference