Skip to main content

tostring

Trait tostring 

Source
pub trait tostring {
    // Provided methods
    fn __tostring(&self) -> string { ... }
    fn __name(&self) -> string { ... }
}
Expand description

Metamethods controlling string conversion.

Provided Methods§

Source

fn __tostring(&self) -> string

Invoked by tostring() (and therefore by print) to convert the value to a string.

§Examples
local pt = setmetatable({ x = 1, y = 2 }, {
  __tostring = function(p) return "(" .. p.x .. ", " .. p.y .. ")" end,
})
print(pt)    --> (1, 2)
Source

fn __name(&self) -> string

A string naming the type of the value, used in error messages and by tostring when __tostring is absent.

§Examples
local obj = setmetatable({}, { __name = "Point" })
print(obj)    --> Point: 0x...

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§