Skip to content

Commit 93ba558

Browse files
author
Alfie John
committed
doc: adding example for implementations without traits
1 parent 9830051 commit 93ba558

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/doc/reference.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,20 @@ methods in such an implementation can only be used as direct calls on the
16841684
values of the type that the implementation targets. In such an implementation,
16851685
the trait type and `for` after `impl` are omitted. Such implementations are
16861686
limited to nominal types (enums, structs), and the implementation must appear
1687-
in the same module or a sub-module as the `self` type.
1687+
in the same module or a sub-module as the `self` type:
1688+
1689+
```
1690+
struct Point {x: int, y: int}
1691+
1692+
impl Point {
1693+
fn log(&self) {
1694+
println!("Point is at ({}, {})", self.x, self.y);
1695+
}
1696+
}
1697+
1698+
let my_point = Point {x: 10, y:11};
1699+
my_point.log();
1700+
```
16881701

16891702
When a trait _is_ specified in an `impl`, all methods declared as part of the
16901703
trait must be implemented, with matching types and type parameter counts.

0 commit comments

Comments
 (0)