You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rustlings/exercises/conversions
Greg Leonard 2933f51949
chore: Change point to comma in from_into.rs
A typo in the fn test_bad_age() hint message had a point rather than comma

Prev:
// Test that "Mark.twenty"

Current:
// Test that "Mark,twenty"
4 years ago
..
README.md feat: Add type conversion and parsing exercises 5 years ago
as_ref_mut.rs chore: Alter whitespace for consistency 4 years ago
from_into.rs chore: Change point to comma in from_into.rs 4 years ago
from_str.rs chore: Run rustfmt on exercises 4 years ago
try_from_into.rs chore: Run rustfmt on exercises 4 years ago
using_as.rs fix(using_as): Add test so that proper type is returned. (#512) 4 years ago

README.md

Type conversions

Rust offers a multitude of ways to convert a value of a given type into another type.

The simplest form of type conversion is a type cast expression. It is denoted with the binary operator as. For instance, println!("{}", 1 + 1.0); would not compile, since 1 is an integer while 1.0 is a float. However, println!("{}", 1 as f32 + 1.0) should compile. The exercise using_as tries to cover this.

Rust also offers traits that facilitate type conversions upon implementation. These traits can be found under the convert module. The traits are the following:

Furthermore, the std::str module offers a trait called FromStr which helps with converting strings into target types via the parse method on strings. If properly implemented for a given type Person, then let p: Person = "Mark,20".parse().unwrap() should both compile and run without panicking.

These should be the main ways within the standard library to convert data into your desired types.

Book Sections

These are not directly covered in the book, but the standard library has great documentation for conversions here. The FromStr trait is also covered here.