Convert and format values
str.from converts numbers or booleans to text, returning
Result[str, AllocError]. Floats use the shortest text that parses back to the
same value.
def main() -> Result[(), AllocError | IoError]:
print(str.from(42)?)?
print(str.from(-0.0f32)?)?
print(str.from(True)?)?
Ok(())
42
-0.0
True
Inspect a value with str.repr
str.repr formats a borrowed value; print writes a value and newline,
reporting formatting allocation failures and output errors:
def main() -> Result[(), AllocError | IoError]:
values = [1, 2]?
print(str.repr(values)?)?
print(values)?
Ok(())
[1, 2]
[1, 2]
Formatting failure writes nothing. An output error can leave a partial write. The source remains usable; neither call copies a mutable collection.
The inner ? handles formatting; the outer ? handles printing. To inspect a
Result wrapper itself, pass it to str.repr:
def main() -> Result[(), AllocError | IoError]:
result: Result[i64, str] = Err("not ready")
print(str.repr(result)?)?
Ok(())
Result[i64, str].Err("not ready")