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

Tutorial additions, restructuring, simplification #4240

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6a778ca
doc: 'trait constraints' -> 'inheritance'. Expand
brson Dec 20, 2012
8795e8b
tutorial: Reorder sections on traits
brson Dec 20, 2012
4cc8877
tutorial: Rewrite method section to deal with explicit self
brson Dec 20, 2012
68b80e4
Use explicit self in rest of tutorial
brson Dec 20, 2012
cad30a2
Move mast static method to the section on methods
brson Dec 20, 2012
0617708
tutorial: Move method discussion after closures, before generics
brson Dec 20, 2012
d468893
tutorial: Typos
brson Dec 20, 2012
0a7bd78
tutorial: Update for moves based on type
brson Dec 20, 2012
7ec45b6
tutorial: update intro
brson Dec 20, 2012
91895ad
tutorial: Fix formatting
brson Dec 20, 2012
614629a
tutorial: Remove confusing discussion about semicolons
brson Dec 20, 2012
ecd1aa7
tutorial: Remove mutable vector syntax
brson Dec 20, 2012
4855ce1
tutorial: Remove the entire 'Types' section
brson Dec 20, 2012
8ea7682
tutorial: Remove the section on constants
brson Dec 20, 2012
685b6fb
tutorial: Integrate constants into main intro text
brson Dec 20, 2012
41aee4d
tutorial: It doesn't matter that Rust identifiers are the same as C
brson Dec 20, 2012
42f37e0
tutorial: Discuss the primitive types along with their literals
brson Dec 20, 2012
e971595
tutorial: Mention rusti with other tools
brson Dec 20, 2012
89caa31
tutorial: Update scope
brson Dec 20, 2012
1b2af16
tutorial: Fix example in syntax basics section
brson Dec 20, 2012
51c8ba0
tutorial: Editing
brson Dec 20, 2012
28aae89
tutorial: Remove some trivia about operators
brson Dec 20, 2012
a9bb1c9
tutorial: Try to fit the early discussion of :: in better
brson Dec 20, 2012
993b724
tutorial: Clean up language about syntax extensions
brson Dec 20, 2012
3113b73
tutorial: Typo
brson Dec 20, 2012
44130c1
tutorial: Re-remove core:: prefix from some examples
brson Dec 20, 2012
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
29 changes: 28 additions & 1 deletion doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1222,17 +1222,44 @@ impl float: Num {
let x: float = Num::from_int(42);
~~~~

Traits can have _constraints_ for example, in
Traits may inherit from other traits. For example, in

~~~~
trait Shape { fn area() -> float; }
trait Circle : Shape { fn radius() -> float; }
~~~~

the syntax `Circle : Shape` means that types that implement `Circle` must also have an implementation for `Shape`.
Multiple supertraits are separated by spaces, `trait Circle : Shape Eq { }`.
In an implementation of `Circle` for a given type `T`, methods can refer to `Shape` methods,
since the typechecker checks that any type with an implementation of `Circle` also has an implementation of `Shape`.

In type-parameterized functions,
methods of the supertrait may be called on values of subtrait-bound type parameters.
Refering to the previous example of `trait Circle : Shape`:

~~~
# trait Shape { fn area() -> float; }
# trait Circle : Shape { fn radius() -> float; }
fn radius_times_area<T: Circle>(c: T) -> float {
// `c` is both a Circle and a Shape
c.radius() * c.area()
}
~~~

Likewise, supertrait methods may also be called on trait objects.

~~~ {.xfail-test}
# trait Shape { fn area() -> float; }
# trait Circle : Shape { fn radius() -> float; }
# impl int: Shape { fn area() -> float { 0.0 } }
# impl int: Circle { fn radius() -> float { 0.0 } }
# let mycircle = 0;

let mycircle: Circle = @mycircle as @Circle;
let nonsense = mycircle.radius() * mycircle.area();
~~~

### Implementations

An _implementation_ is an item that implements a [trait](#traits) for a specific type.
Expand Down
Loading