From 76984270da9b3413b05b216abcfbcd09f51cf326 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 10 Dec 2012 20:56:25 -0800 Subject: [PATCH 1/2] Discuss the Option type a bit in the tutorial as per #3577 --- doc/tutorial.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 6927c82125b64..3701dc75160a5 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -1817,14 +1817,29 @@ struct Stack { elements: ~[mut T] } -enum Maybe { - Just(T), - Nothing +enum Option { + Some(T), + None } ~~~~ These declarations can be instantiated to valid types like `Set`, -`Stack` and `Maybe`. +`Stack` and `Option`. + +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` instead of `T`. + +~~~~ +fn radius(shape: Shape) -> Option { + 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 From 86205a302362262cd4f2395ecb47b0232db040ad Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 10 Dec 2012 21:17:31 -0800 Subject: [PATCH 2/2] Discuss module syntax earlier in the tutorial as per #3578 --- doc/tutorial.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/tutorial.md b/doc/tutorial.md index 3701dc75160a5..b58ba6c3222ed 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -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.