Skip to content

rust Namespace

Derek Snider edited this page May 19, 2026 · 1 revision

rust:: Namespace

18 Rust-inspired string and array helper functions. This is namespace sugar over madc's string and array types — it does not add Rust ownership, borrowing, or lifetimes.

See also: Namespaces Overview | Other namespaces | rust::match

String Functions

Function Description Example
trim(str) Trim both ends in place rust::trim(s)
trim_start(str) Trim the left side in place rust::trim_start(s)
trim_end(str) Trim the right side in place rust::trim_end(s)
contains(str, needle) Returns 1 if needle is present if ( rust::contains(s, "foo") )
starts_with(str, prefix) Returns 1 if str starts with prefix rust::starts_with(s, "http")
ends_with(str, suffix) Returns 1 if str ends with suffix rust::ends_with(s, ".txt")
replace(str, from, to) Replace all occurrences in place rust::replace(s, "foo", "bar")
repeat(str, count) Repeat the string in place rust::repeat(s, 3)
len(str) Returns string length n = rust::len(s)
is_empty(str) Returns 1 if empty if ( rust::is_empty(s) )

Array Functions

Function Description Example
split(arr, str, delim) Split string by delimiter into array rust::split(a, s, ",")
split_whitespace(arr, str) Split on runs of whitespace rust::split_whitespace(a, s)
join(result, arr, sep) Join array elements into string rust::join(s, a, "-")
first(result, arr) Copy first element rust::first(s, a)
last(result, arr) Copy last element rust::last(s, a)
get(result, arr, idx) Copy indexed element rust::get(s, a, 2)
push(arr, value) Append a string value rust::push(a, "val")
pop(result, arr) Pop last element rust::pop(s, a)

Example

#include <iostream>

int main()
{
    string s = "  hello rust  ";
    rust::trim(s);

    array parts;
    string delim = " ";
    rust::split(parts, s, delim);

    string out;
    string sep = "-";
    rust::join(out, parts, sep);
    cout << out << endl;    // hello-rust

    int has;
    string needle = "rust";
    has = rust::contains(out, needle);
    cout << has << endl;    // 1

    return 0;
}

rust::match

The rust:: namespace also provides a match statement — a no-fall-through alternative to switch.

← Back to Namespaces | ← Back to Home

Clone this wiki locally