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
22 changes: 13 additions & 9 deletions src/generics/impl.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,43 @@ Similar to functions, implementations require care to remain generic.

```rust
struct S; // Concrete type `S`
struct GenericVal<T>(T,); // Generic type `GenericVal`
struct GenericVal<T>(T); // Generic type `GenericVal`

// impl of GenericVal where we explicitly specify type parameters:
impl GenericVal<f32> {} // Specify `f32`
impl GenericVal<S> {} // Specify `S` as defined above

// `<T>` Must precede the type to remain generic
impl <T> GenericVal<T> {}
impl<T> GenericVal<T> {}
```

```rust,editable
struct Val {
val: f64
val: f64,
}

struct GenVal<T>{
gen_val: T
struct GenVal<T> {
gen_val: T,
}

// impl of Val
impl Val {
fn value(&self) -> &f64 { &self.val }
fn value(&self) -> &f64 {
&self.val
}
}

// impl of GenVal for a generic type `T`
impl <T> GenVal<T> {
fn value(&self) -> &T { &self.gen_val }
impl<T> GenVal<T> {
fn value(&self) -> &T {
&self.gen_val
}
}

fn main() {
let x = Val { val: 3.0 };
let y = GenVal { gen_val: 3i32 };

println!("{}, {}", x.value(), y.value());
}
```
Expand Down