Examples
Rust in a program
About
Rust in a Quillo program.
quillo run rust.qwe --watch then open http://localhost:8080
rust.qwer, next to this file, is Rust. use builds it the first time (a few
seconds; it needs Rust installed, or quillo build rust.qwe done
somewhere it is) and loads it, and its pub fns are called like any other
function: numbers, text, lists and maps go in and come back, a Result's
error is an error value, and a panic is an error in that request alone.
The checker knows their types from the Rust. A call runs off the event loop,
so a long one holds up no one. Change rust.qwer and --watch builds it again.
Run it
$ quillo run rust.qwe --watchrust.qwe
rust.qwe
use "rust.qwer"server fn count_primes(n: int) -> map { let started = now() let r = primes(n) merge(r, { ms: now() - started })}server fn collatz(n: int) -> map ! str { let started = now() let [number, length] = longest_collatz(n)? { number, length, ms: now() - started }}component Lab() { state n = 10000000 state primes_found = null state chain = null state problem = "" <section.lab> <label>Up to <input type="number" bind:value={n} min="2"/></label> <div.buttons> <button @click={() => { primes_found = count_primes(int(n)) }}>Count primes</button> <button @click={() => { let r = collatz(int(n)) problem = is_err(r) ? r.error : "" chain = is_err(r) ? null : r }}>Longest Collatz chain</button> </div> {primes_found != null && <p> <b>{primes_found.count}</b> primes below {n}, the largest <b>{primes_found.largest}</b> ({primes_found.ms} ms) </p>} {chain != null && <p> Below {n}, <b>{chain.number}</b> takes the most steps to reach 1: <b>{chain.length}</b> ({chain.ms} ms) </p>} {problem != "" && <p.problem>{problem}</p>} </section>}let style = css`body { font: 16px/1.5 system-ui, sans-serif; max-width: 40rem; margin: 3rem auto; padding: 0 1rem; color: #1f2328; }input { font: inherit; width: 10rem; margin-left: .5rem; padding: .3rem .5rem; }.buttons { display: flex; gap: .5rem; margin: 1rem 0; }button { font: inherit; padding: .4rem .9rem; border-radius: 6px; border: 1px solid #d0d7de; background: #f6f8fa; cursor: pointer; }.problem { color: #cf222e; }`route GET / = <html> <head><meta charset="utf-8"/><title>Rust in Quillo</title><style>{style}</style></head> <body><h1>Rust in Quillo</h1><Lab/></body> </html>rust.qwer
rust.qwer
//! Number work for rust.qwe, in Rust.//!//! Every top-level `pub fn` here is a function of the programs that//! `use "rust.qwer"`: Quillo builds this file (once per content) and loads it.use std::collections::HashMap;/// How many primes there are below `n`, and the largest of them.pub fn primes(n: i64) -> HashMap<String, i64> { let n = n.clamp(0, 200_000_000) as usize; let mut composite = vec![false; n]; let (mut count, mut largest) = (0, 0); for i in 2..n { if !composite[i] { count += 1; largest = i as i64; let mut j = i * i; while j < n { composite[j] = true; j += i; } } } HashMap::from([("count".to_string(), count), ("largest".to_string(), largest)])}/// The number below `n` whose Collatz sequence is longest, and its length.pub fn longest_collatz(n: i64) -> Result<Vec<i64>, String> { if n < 2 || n > 50_000_000 { return Err(format!("{} is out of range (2 to 50,000,000)", n)); } let n = n as usize; let mut steps = vec![0u32; n]; let (mut best, mut best_len) = (1, 1); for start in 2..n { let (mut x, mut len) = (start as u64, 0u32); while x >= start as u64 { x = if x % 2 == 0 { x / 2 } else { 3 * x + 1 }; len += 1; } steps[start] = len + steps[x as usize]; if steps[start] + 1 > best_len { best = start; best_len = steps[start] + 1; } } Ok(vec![best as i64, best_len as i64])}Thread
Thread 0 posts
On Rust in a program: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.