From 8b82ec984beccf0203bf98a313e1b75ba092c801 Mon Sep 17 00:00:00 2001 From: Nevroz Arslan Date: Tue, 7 Apr 2026 14:06:23 +0300 Subject: [PATCH] Improve wording in From/Into c-cpp-book Clarify From/Into relationship in tutorial Improve the wording around the `From` and `Into` relationship in `ch11-from-and-into-traits.md`. Also correct the commented alternate conversion example from `(40.42)::into()` to `let p: Point = (40,42).into();`. --- c-cpp-book/src/ch11-from-and-into-traits.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/c-cpp-book/src/ch11-from-and-into-traits.md b/c-cpp-book/src/ch11-from-and-into-traits.md index 43d5a62..6a4c2c4 100644 --- a/c-cpp-book/src/ch11-from-and-into-traits.md +++ b/c-cpp-book/src/ch11-from-and-into-traits.md @@ -3,7 +3,8 @@ > **What you'll learn:** Rust's type conversion traits — `From` and `Into` for infallible conversions, `TryFrom` and `TryInto` for fallible ones. Implement `From` and get `Into` for free. Replaces C++ conversion operators and constructors. - ```From``` and ```Into``` are complementary traits to facilitate type conversion -- Types normally implement the ```From``` trait. The ```String::from()``` converts from "&str" to ```String```, and the compiler can automatically derive ```&str.into``` +- Types normally implement the ```From``` trait.```String::from("Rust")``` converts a ```&str``` into a ```String```. +When ```From for U``` exists, Rust also provides ```Into for T```, so ```let s: String = "Rust".into();``` works too. ```rust struct Point {x: u32, y: u32} // Construct a Point from a tuple @@ -16,7 +17,7 @@ fn main() { let s = String::from("Rust"); let x = u32::from(true); let p = Point::from((40, 42)); - // let p : Point = (40.42)::into(); // Alternate form of the above + // let p : Point = (40,42).into(); // Alternate form of the above println!("s: {s} x:{x} p.x:{} p.y {}", p.x, p.y); } ```