官术网_书友最值得收藏!

  • Mastering Rust
  • Rahul Sharma Vesa Kaihlavirta
  • 296字
  • 2021-07-02 13:35:29

Generic implementations

We can also write impl blocks for our generic types too, but it gets verbose here because of the extra generic type parameters, as we'll see. Let's implement a new() method on our Container<T> struct:

// generic_struct_impl.rs

struct Container<T> {
item: T
}

impl Container<T> {
fn new(item: T) -> Self {
Container { item }
}
}

fn main() {
// stuff
}

Let's compile this:

The error message cannot find our generic type T. When writing an impl block for any generic type, we need to declare the generic type parameter before using it within our type. T is just like a variable—a type variable—and we need to declare it. Therefore, we need to modify the implementation block a bit by adding <T> after impl, like so:

impl<T> Container<T> {
fn new(item: T) -> Self {
Container { item }
}
}

With that change, the preceding code compiles. The previous impl block  basically means that we are implementing these methods for all types T, which appear in Container<T>. This impl block is a generic implementation. Therefore, every concrete Container that ever gets generated will have these methods. Now, we could have also written a more specific impl block for Container<T> by putting any concrete type in place of T. This is what it would look like:

impl Container<u32> {
fn sum(item: u32) -> Self {
Container { item }
}
}

In the preceding code, we implemented a method called sum, which is only present on Container<u32> types. Here, we don't need the <T> after impl because of the presence of u32 as a concrete type. This is another nice property of impl blocks, which allows you to specialize generic types by implementing methods independently.

主站蜘蛛池模板: 扶绥县| 泸州市| 河池市| 商城县| 固镇县| 吉木乃县| 三台县| 汉川市| 甘泉县| 柯坪县| 深泽县| 衡阳市| 嵩明县| 霍州市| 温州市| 宝清县| 湖南省| 雅江县| 石嘴山市| 乌拉特后旗| 建始县| 饶平县| 封丘县| 休宁县| 闽侯县| 平顺县| 河西区| 湟中县| 洪湖市| 凤山县| 平安县| 岗巴县| 四平市| 霞浦县| 奉节县| 神农架林区| 平南县| 泰和县| 丰顺县| 昔阳县| 伊通|