Module std::fmt

Number formatting and string padding utilities.

Provides integer-to-string conversions for hexadecimal, binary, and octal bases, plus helpers for padding and repeating strings.

Examples

import std::fmt;

fn main() {
    println(fmt.to_hex(255));         // "ff"
    println(fmt.to_binary(10));       // "1010"
    println(fmt.to_octal(8));         // "10"
    println(fmt.pad_left("7", 3, "0"));  // "007"
}

Contents

Functions

Function to_hex

pub fn to_hex(n: i64) -> string

Convert an integer to its lowercase hexadecimal string representation.

Negative numbers are prefixed with -.

Examples

fmt.to_hex(255)   // "ff"
fmt.to_hex(0)     // "0"
fmt.to_hex(-16)   // "-10"

Function to_binary

pub fn to_binary(n: i64) -> string

Convert an integer to its binary string representation.

Negative numbers are prefixed with -.

Examples

fmt.to_binary(10)   // "1010"
fmt.to_binary(0)    // "0"
fmt.to_binary(-5)   // "-101"

Function to_octal

pub fn to_octal(n: i64) -> string

Convert an integer to its octal string representation.

Negative numbers are prefixed with -.

Examples

fmt.to_octal(8)    // "10"
fmt.to_octal(0)    // "0"
fmt.to_octal(-9)   // "-11"

Function pad_left

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

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

If s is already at least width characters, returns s unchanged. Delegates to string.pad_left — prefer that form when importing std::string directly; fmt.pad_left is a convenience for code that already imports fmt.

Examples

fmt.pad_left("42", 5, " ")   // "   42"
fmt.pad_left("42", 5, "0")   // "00042"

Function pad_right

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

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

If s is already at least width characters, returns s unchanged. Delegates to string.pad_right — prefer that form when importing std::string directly; fmt.pad_right is a convenience for code that already imports fmt.

Examples

fmt.pad_right("hi", 5, ".")   // "hi..."

Function repeat

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

Repeat a string count times.

Examples

fmt.repeat("ab", 3)   // "ababab"
fmt.repeat("*", 5)    // "*****"