Home / Docs / Standard library / Prelude
to_int
A function of the prelude, in scope in every module. Edition 0.1 and later.
pub to_int(t: Text) -> Option[Int]Some of the integer that t writes when t is an optional - followed by one or more ASCII digits, leading zeros allowed, and None for any other text: the empty text, "-", a leading +, whitespace or digits outside ASCII. Int has no bound, so a long text of digits is never None for its length (spec/14 §1).
Parameters
t: the text
Returns
Option[Int]: Some of the integer t writes, an optional - and from 1 to 4300 ASCII digits; None for any other text, among them a leading + and more than 4300 digits.
Use instead
parse_int(edition 0.2 on) to know why a text is not a number
See also
Example
module shop
--- A stock adjustment typed by hand: a sign is allowed, so `-3` writes off three units.
adjustment(raw: Text) -> Text
=
when to_int(raw)
is Some(n) -> "'{raw}': adjust the stock by {n}"
is None -> "'{raw}': not an adjustment"
pub main() -> Unit
uses @console: Console[print]
=
@console.print(adjustment("12"))
@console.print(adjustment("-3"))
@console.print(adjustment("+3"))una run . prints:
'12': adjust the stock by 12
'-3': adjust the stock by -3
'+3': not an adjustmentSpecification
Generated from the standard modules of una v0.4.0 (docs/std/listing.json, which is not part of the specification). The specification governs: spec/14 §1.