Standard Library
Reference for Sounio's standard library
Standard Library
Sounio’s standard library provides essential types, functions, and modules for scientific computing.
Core Modules
std::prelude
Automatically imported in every file:
- Basic types:
i32,f64,bool,String, etc. - Common traits:
Clone,Debug,Eq,Ord - Option and Result types
- Knowledge types
std::collections
Data structures for scientific computing:
use std::collections::{Vec, HashMap, HashSet, BTreeMap}
let numbers: Vec<i32> = vec![1, 2, 3, 4, 5]
let map: HashMap<String, f64> = HashMap::new()
std::math
Mathematical functions and constants:
use std::math::{sin, cos, tan, sqrt, exp, ln, log10, PI, E}
let angle = PI / 4.0
let result = sin(angle).pow(2) + cos(angle).pow(2) // ≈ 1.0
std::stats
Statistical functions:
use std::stats::{mean, median, std_dev, variance, correlation}
let data = vec![1.0, 2.0, 3.0, 4.0, 5.0]
let avg = mean(&data) // 3.0
let sd = std_dev(&data) // 1.58...
let med = median(&data) // 3.0
std::linalg
Linear algebra operations:
use std::linalg::{Matrix, Vector, dot, cross, solve}
let a = Matrix::from([
[1.0, 2.0],
[3.0, 4.0],
])
let b = Vector::from([5.0, 6.0])
let x = solve(a, b) // Solves Ax = b
std::units
Physical units and dimensional analysis:
use std::units::{kg, m, s, N, J}
let mass: kg = 10.0
let accel: m/s^2 = 9.81
let force: N = mass * accel // 98.1 N
See Units of Measure for details.
std::epistemic
Epistemic computing primitives:
use std::epistemic::{Knowledge, measure, observe, Provenance}
let temp: Knowledge<f64> = measure(
value: 23.5,
uncertainty: 0.2,
source: "sensor_A"
)
See Knowledge Types for details.
I/O Module
std::io
Input/output operations:
use std::io::{read_file, write_file, stdin, stdout}
fn main() with IO {
let content = read_file("data.txt")
write_file("output.txt", process(content))
}
std::fs
Filesystem operations:
use std::fs::{exists, create_dir, remove_file, read_dir}
if !exists("output") {
create_dir("output")
}
Data Formats
std::json
JSON parsing and serialization:
use std::json::{parse, stringify, Json}
let data: Json = parse(json_string)?
let value = data["key"].as_string()?
let output = stringify(data)
std::csv
CSV file handling:
use std::csv::{read_csv, write_csv, CsvReader}
let reader = CsvReader::open("data.csv")?
for row in reader {
let name = row.get("name")?
let value: f64 = row.parse("value")?
}
Scientific Computing
std::ode
Ordinary differential equation solvers:
use std::ode::{rk45, euler, solve_ivp}
let solution = rk45(
f: |t, y| -0.5 * y, // dy/dt = -0.5y
t_span: (0.0, 10.0),
y0: 1.0,
)
std::optimize
Optimization algorithms:
use std::optimize::{minimize, maximize, gradient_descent}
let result = minimize(
f: |x| (x - 3.0).pow(2),
x0: 0.0,
method: GradientDescent { lr: 0.1 },
)
std::fft
Fast Fourier Transform:
use std::fft::{fft, ifft, rfft}
let signal = vec![1.0, 0.0, -1.0, 0.0]
let spectrum = fft(&signal)
Concurrency
std::async
Asynchronous programming:
use std::async::{spawn, await_all, channel}
fn main() with IO, Async {
let tasks = (0..10).map(|i| spawn(|| compute(i)))
let results = await_all(tasks)
}
std::sync
Synchronization primitives:
use std::sync::{Mutex, RwLock, Arc, Barrier}
let shared = Arc::new(Mutex::new(0))
GPU Computing
std::gpu
GPU kernel support:
use std::gpu::{kernel, launch, Device}
#[kernel]
fn vector_add(a: &[f32], b: &[f32], c: &![f32]) {
let i = thread_idx()
c[i] = a[i] + b[i]
}
See GPU Computing for details.