From 93ba558588c3c3c406d2ba923794550567c66bed Mon Sep 17 00:00:00 2001 From: Alfie John Date: Fri, 21 Nov 2014 04:43:07 +0000 Subject: [PATCH] doc: adding example for implementations without traits --- src/doc/reference.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/doc/reference.md b/src/doc/reference.md index 4bba6bef5cfe0..d6f9c3b3a5547 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -1684,7 +1684,20 @@ methods in such an implementation can only be used as direct calls on the values of the type that the implementation targets. In such an implementation, the trait type and `for` after `impl` are omitted. Such implementations are limited to nominal types (enums, structs), and the implementation must appear -in the same module or a sub-module as the `self` type. +in the same module or a sub-module as the `self` type: + +``` +struct Point {x: int, y: int} + +impl Point { + fn log(&self) { + println!("Point is at ({}, {})", self.x, self.y); + } +} + +let my_point = Point {x: 10, y:11}; +my_point.log(); +``` When a trait _is_ specified in an `impl`, all methods declared as part of the trait must be implemented, with matching types and type parameter counts.