Tsallaka zuwa abun ciki
Justice Tech School

Formatting and to_string

format! returns a String. to_string and parse convert between text and numbers, and parse needs a type.

Example
let n: i32 = "42".parse().unwrap();
let f = "2.5".parse::<f64>().unwrap();
println!("{}", n + 1);
println!("{}", f * 2.0);
let s = format!("{}-{:03}", "id", 7);
println!("{} {}", s, 99.to_string());

Result

43
5
id-007 99

Exercise

Convert text to a number.

"5".::<i32>()

Quiz Test

Which builds a String?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.