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

Two tutorial fixes - #3577 and #3578 #4157

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
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
28 changes: 24 additions & 4 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ while count < 10 {
}
~~~~

The name of the function that prints a line of text, `io::println`, is
qualified: it refers to the function named `println` that's defined in the
module `io`. In Rust, a double colon---`::`---separates parts of a
qualified name. For more details, see the section on [crates](#crates).

Although Rust can almost always infer the types of local variables, you
can specify a variable's type by following it with a colon, then the type
name.
Expand Down Expand Up @@ -1817,14 +1822,29 @@ struct Stack<T> {
elements: ~[mut T]
}

enum Maybe<T> {
Just(T),
Nothing
enum Option<T> {
Some(T),
None
}
~~~~

These declarations can be instantiated to valid types like `Set<int>`,
`Stack<int>` and `Maybe<int>`.
`Stack<int>` and `Option<int>`.

The last type in that example, `Option`, appears frequently in Rust code.
Because Rust does not have null pointers (except in unsafe code), we need
another way to write a function whose result isn't defined on every possible
combination of arguments of the appropriate types. The usual way is to write
a function that returns `Option<T>` instead of `T`.

~~~~
fn radius(shape: Shape) -> Option<float> {
match shape {
Circle(_, radius) => Some(radius),
Rectangle(*) => None
}
}
~~~~

The Rust compiler compiles generic functions very efficiently by
*monomorphizing* them. *Monomorphization* is a fancy name for a simple
Expand Down