Merge pull request #4669 from pnkfelix/tutorial-revisions
Small fixes to code samples in Tutorials
This commit is contained in:
commit
3c02e2b61e
@ -302,7 +302,7 @@ rejected by the compiler):
|
||||
fn example3() -> int {
|
||||
let mut x = ~X {f: 3};
|
||||
let y = &x.f;
|
||||
x = ~{f: 4}; // Error reported here.
|
||||
x = ~X {f: 4}; // Error reported here.
|
||||
*y
|
||||
}
|
||||
~~~
|
||||
@ -366,12 +366,14 @@ Things get trickier when the unique box is not uniquely owned by the
|
||||
stack frame, or when there is no way for the compiler to determine the
|
||||
box's owner. Consider a program like this:
|
||||
|
||||
~~~
|
||||
~~~ {.xfail-test}
|
||||
struct R { g: int }
|
||||
struct S { mut f: ~R }
|
||||
fn example5a(x: @S ...) -> int {
|
||||
fn example5a(x: @S, callback: @fn()) -> int {
|
||||
let y = &x.f.g; // Error reported here.
|
||||
...
|
||||
callback();
|
||||
...
|
||||
# return 0;
|
||||
}
|
||||
~~~
|
||||
|
@ -2015,7 +2015,7 @@ the method name with the trait name.
|
||||
The compiler will use type inference to decide which implementation to call.
|
||||
|
||||
~~~~
|
||||
# trait Shape { static fn new(area: float) -> self; }
|
||||
trait Shape { static fn new(area: float) -> self; }
|
||||
# use float::consts::pi;
|
||||
# use float::sqrt;
|
||||
struct Circle { radius: float }
|
||||
@ -2211,11 +2211,15 @@ Likewise, supertrait methods may also be called on trait objects.
|
||||
~~~ {.xfail-test}
|
||||
# trait Shape { fn area(&self) -> float; }
|
||||
# trait Circle : Shape { fn radius(&self) -> float; }
|
||||
# impl int: Shape { fn area(&self) -> float { 0.0 } }
|
||||
# impl int: Circle { fn radius(&self) -> float { 0.0 } }
|
||||
# let mycircle = 0;
|
||||
# use float::consts::pi;
|
||||
# use float::sqrt;
|
||||
# struct Point { x: float, y: float }
|
||||
# struct CircleStruct { center: Point, radius: float }
|
||||
# impl CircleStruct: Circle { fn radius(&self) -> float { sqrt(self.area() / pi) } }
|
||||
# impl CircleStruct: Shape { fn area(&self) -> float { pi * square(self.radius) } }
|
||||
|
||||
let mycircle: Circle = @mycircle as @Circle;
|
||||
let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f};
|
||||
let mycircle: Circle = concrete as @Circle;
|
||||
let nonsense = mycircle.radius() * mycircle.area();
|
||||
~~~
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user