Why `Box` can inherit its generic's methods?

Why some type like Box can inherit its generic's methods?
For example,

let box = Box::new(String::from("ABCD"));
box.push_str("EFG");

Box contains a String can be used as a String.
Arc type can also do that.

What is this? Is there any syntax to do this?

It's because Box<T> implements the Deref trait, which allows the compiler to essentially implicitly convert &Box<T> to &T. See the chapter about Deref in TRPL.

2 Likes

Thanks very much!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.