Module std::string

string conversion and manipulation utilities.

Most string operations (.find(), .slice(), .trim(), etc.) are built-in methods available on all strings. This module provides type-conversion helpers and additional string utilities.

Examples

import std.string;

fn main() {
    let s = string.from_int(42);        // "42"
    let n = string.to_int("42").expect("to_int succeeds");       // 42
    let f = string.to_float("3.14").expect("to_float succeeds");     // 3.14
    let nl = string.from_char(10);      // "\n"
    let stars = string.repeat("*", 3);  // "***"
    let padded = string.pad_left("7", 3, "0");  // "007"
}

Contents

Functions

Function from_int

pub fn from_int(n: i64) -> string

Convert an integer to its string representation.

Examples

import std.string;

fn main() {
    let s = string.from_int(42);  // "42"
}

Function from_float

pub fn from_float(f: f64) -> string

Convert a float to its string representation.

Function from_bool

pub fn from_bool(b: bool) -> string

Convert a bool to "true" or "false".

Function from_char

pub fn from_char(code: i64) -> string

Convert a character code to a single-character string.

Examples

import std.string;

fn main() {
    let newline = string.from_char(10);
}

Function to_int

pub fn to_int(s: string) -> Result<i64, string>

Unlike C's atoi, this function does not stop at the first non-digit character — any non-digit anywhere in the string (after an optional leading sign) returns a structured error.

Function to_float

pub fn to_float(s: string) -> Result<f64, string>

Parse a string as a float, returning a structured error on failure.

Function is_empty

pub fn is_empty(s: string) -> bool

Check if a string is empty.

Examples

import std.string;

fn main() {
    println(string.is_empty(""));   // true
    println(string.is_empty("hi"));   // false
}

Function repeat

pub fn repeat(s: string, n: i64) -> string

Repeat a string n times.

Examples

import std.string;

fn main() {
    let stars = string.repeat("*", 5);  // "*****"
}

Function pad_left

pub fn pad_left(s: string, width: i64, pad: string) -> string

Pad a string on the left to reach the given width.

If s is already at least width characters, returns s unchanged.

Examples

import std.string;

fn main() {
    println(string.pad_left("42", 5, " "));   // "   42"
    println(string.pad_left("42", 5, "0"));   // "00042"
}

Function pad_right

pub fn pad_right(s: string, width: i64, pad: string) -> string

Pad a string on the right to reach the given width.

If s is already at least width characters, returns s unchanged.

Examples

import std.string;

fn main() {
    println(string.pad_right("hi", 5, " "));   // "hi   "
}

Function is_numeric

pub fn is_numeric(s: string) -> bool

Check if a string contains only ASCII digits (0-9).

Returns false for empty strings.

Examples

import std.string;

fn main() {
    println(string.is_numeric("123"));   // true
    println(string.is_numeric("12a"));   // false
    println(string.is_numeric(""));   // false
}

Function count

pub fn count(haystack: string, needle: string) -> i64

Count the number of non-overlapping occurrences of needle in haystack.

Examples

import std.string;

fn main() {
    println(string.count("abcabc", "abc"));   // 2
    println(string.count("hello", "x"));   // 0
}

Function starts_with

pub fn starts_with(s: string, prefix: string) -> bool

Check if a string starts with the given prefix.

Examples

import std.string;

fn main() {
    println(string.starts_with("hello", "he"));   // true
    println(string.starts_with("hello", "lo"));   // false
}

Function ends_with

pub fn ends_with(s: string, suffix: string) -> bool

Check if a string ends with the given suffix.

Examples

import std.string;

fn main() {
    println(string.ends_with("hello", "lo"));   // true
    println(string.ends_with("hello", "he"));   // false
}

Function contains

pub fn contains(s: string, sub: string) -> bool

Check if a string contains the given substring.

Examples

import std.string;

fn main() {
    println(string.contains("hello world", "world"));   // true
    println(string.contains("hello", "xyz"));   // false
}

Function is_ascii

pub fn is_ascii(s: string) -> bool

Check if a string contains only ASCII characters (code points 0–127).

Examples

import std.string;

fn main() {
    println(string.is_ascii("hello"));   // true
    println(string.is_ascii("héllo"));   // false
}

Function trim

pub fn trim(s: string) -> string

Trim leading and trailing whitespace from a string.

Function replace

pub fn replace(s: string, old: string, new_val: string) -> string

Replace all occurrences of old in s with new_val.

Function split

pub fn split(s: string, sep: string) -> Vec<string>

Split a string by sep into a list of substrings.

An empty separator splits the string into its individual Unicode characters (one element per codepoint). Empty input with an empty separator returns an empty list. A trailing delimiter produces a trailing empty element.

Examples

import std.string;

fn main() {
    let parts = string.split("a,b,c", ",");  // ["a", "b", "c"]
    let trail = string.split("a,b,", ",");   // ["a", "b", ""]
    let chars = string.split("café", "");    // ["c", "a", "f", "é"]
    let empty = string.split("", "");        // []
}

Function lines

pub fn lines(s: string) -> Vec<string>

Split a string into lines, stripping \r\n or \n endings.

Always emits a final element for the text after the last newline (which will be empty when the string ends with a newline), matching the FFI behaviour.

Examples

import std.string;

fn main() {
    let lines = string.lines("foo\nbar");   // ["foo", "bar"]
    let crlf  = string.lines("a\r\nb");    // ["a", "b"]
    let trail = string.lines("foo\n");     // ["foo", ""]
}

Function join

pub fn join(parts: Vec<string>, sep: string) -> string

Join a list of strings with sep between each element.

Returns an empty string for an empty list.

Examples

import std.string;

fn main() {
    let s = string.join(["a", "b", "c"], ", ");  // "a, b, c"
    let e = string.join([], ",");                // ""
}

Traits

Trait ToString

Trait for types that can be converted to a string representation.

Provides a standard interface for string conversion across modules.

Examples

import std.string;

// Implement for custom types:
// impl ToString for MyType {
//     fn to_str(m: MyType) -> string { ... }
// }

Methods

fn to_str(self: Self) -> string

Convert this value to its string representation.