From d6d87699c9f57a0deb9a15e246dc7dfd5e2a0395 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 8 Apr 2020 22:25:35 +0000 Subject: [PATCH 1/2] Add an example of casting error --- src/types/cast.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/types/cast.md b/src/types/cast.md index 350f9d6fe6..7d990976a3 100644 --- a/src/types/cast.md +++ b/src/types/cast.md @@ -22,6 +22,10 @@ fn main() { let integer = decimal as u8; let character = integer as char; + // Error! There are limitations in conversion rules. A float cannot be directly converted to a char. + let character = decimal as char; + // FIXME ^ Comment out this line + println!("Casting: {} -> {} -> {}", decimal, integer, character); // when casting any value to an unsigned type, T, From cb328e7a8b58225d99daec2e20eaa28cee6900c8 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 8 Apr 2020 22:26:04 +0000 Subject: [PATCH 2/2] Reference the casting doc for primitive conversions --- src/conversion.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/conversion.md b/src/conversion.md index 78d9e2d31e..52a96d06c4 100644 --- a/src/conversion.md +++ b/src/conversion.md @@ -1,10 +1,14 @@ # Conversion -Rust addresses conversion between types by the use of [traits]. The generic +Primitive types can be converted to each other through [casting]. + +Rust addresses conversion between custom types (i.e., `struct` and `enum`) +by the use of [traits]. The generic conversions will use the [`From`] and [`Into`] traits. However there are more specific ones for the more common cases, in particular when converting to and from `String`s. +[casting]: types/cast.md [traits]: trait.md [`From`]: https://doc.rust-lang.org/std/convert/trait.From.html [`Into`]: https://doc.rust-lang.org/std/convert/trait.Into.html