Skip to content

Commit b3c7271

Browse files
committed
tutorial: Swap order of data type/function sections. Add method discussion
1 parent 9902135 commit b3c7271

File tree

1 file changed

+100
-58
lines changed

1 file changed

+100
-58
lines changed

doc/tutorial.md

Lines changed: 100 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -651,60 +651,6 @@ For more involved iteration, such as going over the elements of a
651651
collection, Rust uses higher-order functions. We'll come back to those
652652
in a moment.
653653

654-
# Functions
655-
656-
Like all other static declarations, such as `type`, functions can be
657-
declared both at the top level and inside other functions (or modules,
658-
which we'll come back to [later](#modules-and-crates)).
659-
660-
We've already seen several function definitions. They are introduced
661-
with the `fn` keyword, the type of arguments are specified following
662-
colons and the return type follows the arrow.
663-
664-
~~~~
665-
fn repeat(string: &str, count: int) -> ~str {
666-
let mut result = ~"";
667-
for count.times {
668-
result += string;
669-
}
670-
return result;
671-
}
672-
~~~~
673-
674-
The `return` keyword immediately returns from the body of a function. It
675-
is optionally followed by an expression to return. A function can
676-
also return a value by having its top level block produce an
677-
expression.
678-
679-
~~~~
680-
# const copernicus: int = 0;
681-
fn int_to_str(i: int) -> ~str {
682-
if i == copernicus {
683-
return ~"tube sock";
684-
} else {
685-
return ~"violin";
686-
}
687-
}
688-
~~~~
689-
690-
~~~~
691-
# const copernicus: int = 0;
692-
fn int_to_str(i: int) -> ~str {
693-
if i == copernicus { ~"tube sock" }
694-
else { ~"violin" }
695-
}
696-
~~~~
697-
698-
Functions that do not return a value are said to return nil, `()`,
699-
and both the return type and the return value may be omitted from
700-
the definition. The following two functions are equivalent.
701-
702-
~~~~
703-
fn do_nothing_the_hard_way() -> () { return (); }
704-
705-
fn do_nothing_the_easy_way() { }
706-
~~~~
707-
708654
# Basic datatypes
709655

710656
The core datatypes of Rust are structs, enums (tagged unions, algebraic data
@@ -890,13 +836,109 @@ match mytup {
890836
}
891837
~~~~
892838

839+
# Functions and methods
840+
841+
We've already seen several function definitions. Like all other static
842+
declarations, such as `type`, functions can be declared both at the
843+
top level and inside other functions (or modules, which we'll come
844+
back to [later](#modules-and-crates)). They are introduced with the
845+
`fn` keyword, the type of arguments are specified following colons and
846+
the return type follows the arrow.
847+
848+
~~~~
849+
fn repeat(string: &str, count: int) -> ~str {
850+
let mut result = ~"";
851+
for count.times {
852+
result += string;
853+
}
854+
return result;
855+
}
856+
~~~~
857+
858+
The `return` keyword immediately returns from the body of a function. It
859+
is optionally followed by an expression to return. A function can
860+
also return a value by having its top level block produce an
861+
expression.
862+
863+
~~~~
864+
# const copernicus: int = 0;
865+
fn int_to_str(i: int) -> ~str {
866+
if i == copernicus {
867+
return ~"tube sock";
868+
} else {
869+
return ~"violin";
870+
}
871+
}
872+
~~~~
873+
874+
~~~~
875+
# const copernicus: int = 0;
876+
fn int_to_str(i: int) -> ~str {
877+
if i == copernicus { ~"tube sock" }
878+
else { ~"violin" }
879+
}
880+
~~~~
881+
882+
Functions that do not return a value are said to return nil, `()`,
883+
and both the return type and the return value may be omitted from
884+
the definition. The following two functions are equivalent.
885+
886+
~~~~
887+
fn do_nothing_the_hard_way() -> () { return (); }
888+
889+
fn do_nothing_the_easy_way() { }
890+
~~~~
891+
892+
Methods are like functions, except that they are defined for a specific
893+
'self' type (like 'this' in C++). Calling a method is done with
894+
dot notation, as in `my_vec.len()`. Methods may be defined on most
895+
Rust types with the `impl` keyword. As an example, lets define a draw
896+
method on our `Shape` enum.
897+
898+
~~~
899+
struct Point {
900+
x: float,
901+
y: float
902+
}
903+
904+
enum Shape {
905+
Circle(Point, float),
906+
Rectangle(Point, Point)
907+
}
908+
909+
impl Shape {
910+
fn draw() {
911+
match self {
912+
Circle(p, f) => draw_circle(p, f),
913+
Rectangle(p1, p2) => draw_rectangle(p1, p2)
914+
}
915+
}
916+
}
917+
918+
let s = Circle(Point { x: 1f, y: 2f }, 3f };
919+
s.draw();
920+
~~~
921+
922+
This defines an _implementation_ for `Shape` containing a single
923+
method, `draw`. If we wanted we could add additional methods to the
924+
same impl. In most most respects the `draw` method is defined like
925+
any other function, with the exception of the name `self`. `self` is a
926+
special value that is automatically defined in each method, referring
927+
to the value being operated on. We'll discuss methods more in the
928+
context of [traits and generics](#generics).
929+
930+
> ***Note:*** The method definition syntax will change to require
931+
> declaring the self type explicitly, as the first argument.
932+
893933
# The Rust memory model
894934

895935
At this junction let's take a detour to explain the concepts involved
896-
in Rust's memory model. Rust has a very particular approach to
897-
memory management that plays a significant role in shaping the "feel"
898-
of the language. Understanding the memory landscape will illuminate
899-
several of Rust's unique features as we encounter them.
936+
in Rust's memory model. We've seen some of Rust's pointer sigils (`@`,
937+
`~`, and `&`) float by in a few examples, and we aren't going to get
938+
much further without explaining them. Rust has a very particular
939+
approach to memory management that plays a significant role in shaping
940+
the "feel" of the language. Understanding the memory landscape will
941+
illuminate several of Rust's unique features as we encounter them.
900942

901943
Rust has three competing goals that inform its view of memory:
902944

0 commit comments

Comments
 (0)