Skip to main content

quickstart/
quickstart.rs

1//! The README's opening example, kept as a runnable example so it cannot drift from reality.
2//!
3//! `cargo run --example quickstart`
4use steeldb::SteelDb;
5
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let docs = [
8        "Morty Shade defeated Wallace Gale at Ecruteak City during the Indigo Invitational in 2025.",
9        "Bea Strike defeated Iris Draco at Ecruteak City during the Indigo Invitational in 2025.",
10        "Juan Tide defeated Cynthia Ward at Ecruteak City during the Indigo Invitational in 2026.",
11        "A habitat survey recorded Aggron near Sootopolis City at an elevation of 1082 m.",
12        "A habitat survey recorded Salamence near Sootopolis City at an elevation of 2369 m.",
13        "A habitat survey recorded Milotic near Sootopolis City at an elevation of 340 m.",
14        "Milotic is not permitted in Series 1 play for the 2025 season.",
15        "Metagross is permitted in Series 4 play for the 2026 season.",
16    ];
17
18    let db = SteelDb::ingest(docs)?;
19    println!("indexed {} situations", db.len());
20
21    println!("\nyou can ask about:");
22    for c in db.categories() {
23        println!("  {:<14} {}", c.wildcard(), c.words.iter().take(4).cloned().collect::<Vec<_>>().join(", "));
24    }
25
26    // use a category the corpus actually produced — see the list printed above
27    let q = "(and elevation/* (not state/negated))";
28    match db.query(q) {
29        Ok(a) => {
30            println!("\n{q}  ->  {} situations", a.len());
31            for (id, text) in db.resolve(&a).take(2) {
32                // chars(), not a byte slice: `&text[..n]` panics mid-character
33                println!("  [{id}] {}", text.chars().take(56).collect::<String>());
34            }
35        }
36        Err(r) => println!("\n{r}"),
37    }
38
39    // a category this corpus does not have — refused, not answered with an empty list
40    match db.query("survey/*") {
41        Ok(_) => println!("\nunexpected: survey is not a category here"),
42        Err(r) => println!("\n{r}"),
43    }
44
45    match db.query("gene/brca1") {
46        Ok(_) => println!("\nunexpected: that should not exist"),
47        Err(r) => println!("\n{r}"),
48    }
49
50    println!("\nbelief in a denied claim: {}", db.belief("state/negated"));
51    Ok(())
52}