Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions c-cpp-book/src/ch11-from-and-into-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
> **What you'll learn:** Rust's type conversion traits — `From<T>` and `Into<T>` 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<T> for U``` exists, Rust also provides ```Into<U> for T```, so ```let s: String = "Rust".into();``` works too.
```rust
struct Point {x: u32, y: u32}
// Construct a Point from a tuple
Expand All @@ -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);
}
```
Expand Down