Start with the idea
parse is generic: the destination type tells it what grammar to use. Because arbitrary text may be invalid, the result is Result rather than a bare value. A type annotation or turbofish such as parse::<i32>() resolves the target type, after which match, ?, or another deliberate policy handles failure.
Read the code
let parsed: Result<i32, _> = "42".parse();
match parsed {
Ok(value) => println!("{value}"),
Err(error) => println!("{error}"),
}The annotation tells parse which numeric type to produce.
Practice starter
let value = "42".parse();
Work through it
Text arrives as bytes and characters, not as a trusted number. The call "42".parse::<i32>() asks Rust to interpret it using i32's parsing rules. That operation can fail, so it returns Result<i32, ParseIntError>. The annotation Result<i32, _> in the example gives rustc enough information to infer the error type; the underscore does not mean the error disappears. A successful parse produces Ok(42), while invalid text such as "forty-two" produces Err with diagnostic information.
The match in the example deliberately handles both outcomes. It prints the number on success and the error on failure. In a larger function, ? can propagate an error when the surrounding return type supports it, but that still represents a failure path the caller must consider. unwrap would panic instead, so use it only when a violated invariant truly means the program cannot continue, not for routine user input.
Before choosing a parser, decide the target type and valid range. Parsing "300" as u8 fails even though the text is made of digits, because 300 is outside that type's range. Try valid, malformed, and out-of-range strings, then record which branch runs for each. A good parser does not just extract a value; it makes invalid input visible at the boundary.
Separate syntax errors from domain errors. The text -3 may parse successfully as an integer but still be invalid for a positive count. Validate both steps, and return a message that tells the caller which rule was violated.
Text from a user or file is not yet a number, even if it looks like digits. parse::<i32>() attempts to turn a &str into a typed value and returns Result, because the text may be empty, contain letters, or exceed the type's range. The successful branch contains the number; the error branch tells you that the conversion failed. Do not call unwrap() on unpredictable input unless a crash is genuinely your intended response.
Try the strings "42", "-3", "abc", and a number too large for i32. Predict which parse calls succeed. Then handle both Ok and Err with match and give a reader a useful message for invalid input. If whitespace is allowed by your interface, decide whether to trim it before parsing. Parsing is a boundary: after it succeeds, later code can rely on the value's type rather than repeatedly checking the original text.
Try it
Trim and parse an unsigned 32-bit count. Return Err with the text invalid count for malformed, negative, empty, or overflowing input. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
What information is missing?
Reveal the answer
- The numeric target type
- A mutable string
- A loop around parse
Annotate the binding or call parse::<i32>() so inference has a target.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Parsing Values (courseId "basics", lessonId "parsing") Link: https://crabrix.com/learn/basics/parsing/ Goal: Parsing returns Result because input text is not guaranteed to fit the target type. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/parsing) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.