Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generics/impl.md: follow rustfmt style #1236

Merged
merged 1 commit into from
Aug 6, 2019
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