std-method

str::parse

Convert text to a selected target type. Supply a type when inference cannot identify it. Parsing rules depend on that type. Expected stdout (not executed): Ok(42) true Common mistake: Unwrapping untrusted input or assuming whitespace is automatically removed.

Formal Syntax & Structure

rust syntaxrust
fn parse<F: FromStr>(&self) -> Result<F, F::Err>

Working Example

Example Snippetrust
fn main() {    let count = "42".parse::<u32>();    println!("{count:?}");    println!("{}", "-1".parse::<u32>().is_err());}
Rust Reference