Parsing (std.parse)
The std.parse module provides functions for parsing strings into primitive types with proper error handling.
nic
import std.parse;Overview
All parsing functions return Result[T, *String] where:
Ok(value)contains the successfully parsed valueErr(message)contains an error description
Parsing Integers
parse_i32
Parse a string to a 32-bit signed integer.
nic
import std.parse(parse_i32);
match parse_i32("42") {
Ok(n) => println("Parsed: " + int_to_string(n)),
Err(e) => {
println("Error: " + e.as_str());
release e;
},
}
// Handles negative numbers
match parse_i32("-100") {
Ok(n) => println("Negative: " + int_to_string(n)),
Err(e) => release e,
}Errors:
"empty string"- Input was empty"invalid integer"- Could not parse as integer"integer overflow"- Value outside i32 range (-2,147,483,648 to 2,147,483,647)
parse_i64
Parse a string to a 64-bit signed integer.
nic
import std.parse(parse_i64);
match parse_i64("9223372036854775807") {
Ok(n) => println("Large number parsed successfully"),
Err(e) => {
println("Error: " + e.as_str());
release e;
},
}parse_u64
Parse a string to a 64-bit unsigned integer.
nic
import std.parse(parse_u64);
match parse_u64("18446744073709551615") {
Ok(n) => println("Max u64 parsed"),
Err(e) => {
println("Error: " + e.as_str());
release e;
},
}
// Rejects negative numbers
match parse_u64("-1") {
Ok(n) => {},
Err(e) => {
// e contains "negative value for unsigned type"
release e;
},
}Errors:
"empty string"- Input was empty"invalid integer"- Could not parse as integer"negative value for unsigned type"- Input starts with-
Parsing Floating Point
parse_f64
Parse a string to a 64-bit floating point number.
nic
import std.parse(parse_f64);
match parse_f64("3.14159") {
Ok(f) => println("Pi approximation parsed"),
Err(e) => {
println("Error: " + e.as_str());
release e;
},
}
// Scientific notation works
match parse_f64("1.5e-10") {
Ok(f) => println("Scientific notation OK"),
Err(e) => release e,
}Errors:
"empty string"- Input was empty"invalid float"- Could not parse as floating point
Parsing Booleans
parse_bool
Parse a string to a boolean. Accepts multiple formats (case-insensitive):
| Input | Result |
|---|---|
"true", "TRUE", "True" | true |
"false", "FALSE", "False" | false |
"1" | true |
"0" | false |
"yes", "YES", "Yes" | true |
"no", "NO", "No" | false |
nic
import std.parse(parse_bool);
match parse_bool("yes") {
Ok(b) => {
if b {
println("Affirmative!");
}
},
Err(e) => {
println("Error: " + e.as_str());
release e;
},
}
// All these parse to true
let truthy = ["true", "TRUE", "1", "yes", "YES"];
for s in truthy {
match parse_bool(s) {
Ok(b) => assert(b),
Err(_) => {},
}
}Errors:
"empty string"- Input was empty"invalid boolean value"- Input not recognized
Complete Example
nic
import std.parse(parse_i32, parse_f64, parse_bool);
import std.args(Args);
fn main(argc: i32, argv: **char) -> i32 {
let args = Args.from_main(argc, argv);
defer args.deinit();
// Parse command-line arguments with type conversion
match args.get(1) {
Some(count_str) => {
match parse_i32(count_str.as_str()) {
Ok(count) => {
println("Will run " + int_to_string(count) + " iterations");
},
Err(e) => {
println("Invalid count: " + e.as_str());
release e;
return 1;
},
}
},
None => {
println("Usage: program <count>");
return 1;
},
}
return 0;
}API Reference
nic
/// Parse a string to i32.
/// Returns Ok(value) on success, Err(message) on failure.
pub fn parse_i32(s: string) -> Result[i32, *String];
/// Parse a string to i64.
/// Returns Ok(value) on success, Err(message) on failure.
pub fn parse_i64(s: string) -> Result[i64, *String];
/// Parse a string to u64.
/// Returns Ok(value) on success, Err(message) on failure.
pub fn parse_u64(s: string) -> Result[u64, *String];
/// Parse a string to f64.
/// Returns Ok(value) on success, Err(message) on failure.
pub fn parse_f64(s: string) -> Result[f64, *String];
/// Parse a string to bool.
/// Accepts: "true", "false", "1", "0", "yes", "no" (case-insensitive).
/// Returns Ok(value) on success, Err(message) on failure.
pub fn parse_bool(s: string) -> Result[bool, *String];See Also
- Arguments (std.args) - Command-line argument handling
- CLI Flags (std.flags) - Full CLI parsing with type conversion