Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Getting Started

Let’s explore the basics of rok-utils with practical examples.

Case Conversion

Convert strings between different casing styles:

#![allow(unused)]
fn main() {
use rok_utils::{to_snake_case, to_camel_case, to_kebab_case};

let snake = to_snake_case("HelloWorld");    // "hello_world"
let camel = to_camel_case("hello_world");   // "helloWorld"
let kebab = to_kebab_case("HelloWorld");   // "hello-world"
}

Fluent Builder

The Str::of() builder provides a chainable API:

#![allow(unused)]
fn main() {
use rok_utils::str::Str;

let result = Str::of("  Hello World  ")
    .trim()
    .lower()
    .replace(" ", "-")
    .value();

assert_eq!(result, "hello-world");
}

Conditional Transformations

#![allow(unused)]
fn main() {
use rok_utils::str::Str;

let result = Str::of("hello")
    .when(true, |s| s.append(" world"))
    .when(false, |s| s.append(" hidden"))
    .value();

assert_eq!(result, "hello world");
}

Array Utilities

#![allow(unused)]
fn main() {
use rok_utils::arr::{map, filter, find};

let numbers = [1, 2, 3, 4, 5];

let doubled = map(&numbers, |x| x * 2);
// [2, 4, 6, 8, 10]

let evens = filter(&numbers, |x| x % 2 == 0);
// [2, 4]

let found = find(&numbers, |x| *x == 3);
// Some(3)
}

Error Handling

#![allow(unused)]
fn main() {
use rok_utils::{RokError, RokResultExt};

fn find_user(id: u64) -> Result<String, RokError> {
    if id == 42 {
        Ok("Alice".to_string())
    } else {
        Err(RokError::NotFound(format!("User #{id}")))
    }
}

// Add context to errors
let result = find_user(42).context("Database query failed");
}

Next Steps