String Conversion with ard/string
The ard/string module defines the ToString trait, which allows custom types to be converted to strings. This trait is fundamental to Ard’s type system and is used throughout the standard library.
The string module provides:
- ToString trait for implementing custom string conversion
- Integration with
io::printand string interpolation
use ard/string as Str
struct Person { name: Str, age: Int}
impl Str::ToString for Person { fn to_str() Str { "{@name} ({@age})" }}
fn main() { let person = Person { name: "Alice", age: 30 } io::print(person) // Uses to_str() automatically}trait ToString
Section titled “trait ToString”A trait that types can implement to define how they should be converted to strings.
use ard/string as Str
trait ToString { fn to_str() Str}Implementing ToString
Section titled “Implementing ToString”To implement ToString for a custom type, use the impl syntax:
use ard/string as Str
struct Point { x: Int, y: Int}
impl Str::ToString for Point { fn to_str() Str { "({@x}, {@y})" }}Built-in Implementations
Section titled “Built-in Implementations”The following built-in types have to_str() methods:
- Str: Returns itself
- Int: Converts to decimal string representation
- Float: Converts to decimal string representation
- Bool: Returns “true” or “false”
Examples
Section titled “Examples”Implement ToString for Enum
Section titled “Implement ToString for Enum”use ard/string as Struse ard/io
enum Color { red, green, blue}
impl Str::ToString for Color { fn to_str() Str { match @ { Color::red => "Red", Color::green => "Green", Color::blue => "Blue" } }}
fn main() { let color = Color::red io::print(color) // "Red"}Implement ToString for Struct
Section titled “Implement ToString for Struct”use ard/string as Struse ard/io
struct Rectangle { width: Int, height: Int}
impl Str::ToString for Rectangle { fn to_str() Str { "Rectangle {width: {@width}, height: {@height}}" }}
fn main() { let rect = Rectangle { width: 10, height: 20 } io::print(rect) // "Rectangle {width: 10, height: 20}"}Use in String Interpolation
Section titled “Use in String Interpolation”use ard/string as Struse ard/io
struct Date { year: Int, month: Int, day: Int}
impl Str::ToString for Date { fn to_str() Str { "{@year}-{@month}-{@day}" }}
fn main() { let today = Date { year: 2024, month: 1, day: 15 } io::print("Today is {today}")}Use with Functions
Section titled “Use with Functions”use ard/string as Str
fn print_value(val: Str::ToString) { io::print(val)}
fn main() { print_value(42) print_value("hello") print_value(true)}