Strings & Parse
These are good first stdlib modules because they solve simple, familiar problems:
- work with text
- turn text into typed values
std.strings
Use std.strings when you want small text helpers without leaving the beginner path.
cneg
import std.strings as strings;Current API:
strings.len(str) -> intstrings.copy(str) -> strstrings.concat(str, str) -> strstrings.from_int(int) -> strstrings.eq(str, str) -> boolstrings.starts_with(str, str) -> boolstrings.ends_with(str, str) -> bool
Small example
cneg
import std.strings as strings;
fn:int main() {
let left:str = strings.copy("hello");
let number:str = strings.from_int(42);
let joined:str = strings.concat(left, " world");
if strings.starts_with(joined, "hello") == false {
free left;
free number;
free joined;
return 1;
}
print(number);
print(joined);
free left;
free number;
free joined;
return 0;
}What to notice:
strings.copy(...)creates an owned stringstrings.from_int(...)also creates an owned stringstrings.concat(...)creates another owned string- all three should be freed
ownership
strings.copy(...), strings.concat(...), and strings.from_int(...) return owned strings. Free them when you are done.
std.parse
Use std.parse when you have text and want to turn it into a typed value.
cneg
import std.parse as parse;Current API:
parse.to_int(str) -> result intparse.to_bool(str) -> result bool
Small example
cneg
import std.parse as parse;
fn:int main() {
let value:result int = parse.to_int("42");
if value.ok {
return value.value;
}
return 0;
}What to notice:
- parsing can fail
- that is why the return type is
result int - you check
.okbefore using.value
parsing stays explicit
cnegative does not silently invent fallback values for bad input. The parse either succeeds or it does not.
Beginner recommendation
Start with these functions first:
strings.len(...)strings.eq(...)strings.copy(...)parse.to_int(...)
That is enough for many first CLI programs.