Skip to content

Commit cee97f5

Browse files
steveklabnikalexcrichton
authored andcommitted
---
yaml --- r: 153056 b: refs/heads/try2 c: faf5d92 h: refs/heads/master v: v3
1 parent 27a3209 commit cee97f5

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 8877b81f2cccfbbd6720e9ef81a8238fd188a63e
8+
refs/heads/try2: faf5d926ec21efb04d308fc71214e0afa55649d9
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/doc/guide.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,152 @@ of Rust code. For that, we'll need our next concept: functions.
760760

761761
## Functions
762762

763+
You've already seen one function so far, the `main` function:
764+
765+
```{rust}
766+
fn main() {
767+
}
768+
```
769+
770+
This is the simplest possible function declaration. As we mentioned before,
771+
`fn` says 'this is a function,' followed by the name, some parenthesis because
772+
this function takes no arguments, and then some curly braces to indicate the
773+
body. Here's a function named `foo`:
774+
775+
```{rust}
776+
fn foo() {
777+
}
778+
```
779+
780+
So, what about taking arguments? Here's a function that prints a number:
781+
782+
```{rust}
783+
fn print_number(x: int) {
784+
println!("x is: {}", x);
785+
}
786+
```
787+
788+
Here's a complete program that uses `print_number`:
789+
790+
```{rust}
791+
fn main() {
792+
print_number(5);
793+
}
794+
795+
fn print_number(x: int) {
796+
println!("x is: {}", x);
797+
}
798+
```
799+
800+
As you can see, function arguments work very similar to `let` declarations:
801+
you add a type to the argument name, after a colon.
802+
803+
Here's a complete program that adds two numbers together and prints them:
804+
805+
```{rust}
806+
fn main() {
807+
print_sum(5, 6);
808+
}
809+
810+
fn print_sum(x: int, y: int) {
811+
println!("sum is: {}", x + y);
812+
}
813+
```
814+
815+
You separate arguments with a comma, both when you call the function, as well
816+
as when you declare it.
817+
818+
Unlike `let`, you _must_ declare the types of function arguments. This does
819+
not work:
820+
821+
```{ignore}
822+
fn print_number(x, y) {
823+
println!("x is: {}", x + y);
824+
}
825+
```
826+
827+
You get this error:
828+
829+
```{ignore,notrust}
830+
hello.rs:5:18: 5:19 error: expected `:` but found `,`
831+
hello.rs:5 fn print_number(x, y) {
832+
```
833+
834+
This is a deliberate design decision. While full-program inference is possible,
835+
languages which have it, like Haskell, often suggest that documenting your
836+
types explicitly is a best-practice. We agree that forcing functions to declare
837+
types while allowing for inference inside of function bodies is a wonderful
838+
compromise between full inference and no inference.
839+
840+
What about returning a value? Here's a function that adds one to an integer:
841+
842+
```{rust}
843+
fn add_one(x: int) -> int {
844+
x + 1
845+
}
846+
```
847+
848+
Rust functions return exactly one value, and you declare the type after an
849+
'arrow', which is a dash (`-`) followed by a greater-than sign (`>`).
850+
851+
You'll note the lack of a semicolon here. If we added it in:
852+
853+
```{ignore}
854+
fn add_one(x: int) -> int {
855+
x + 1;
856+
}
857+
```
858+
859+
We would get an error:
860+
861+
```{ignore,notrust}
862+
note: consider removing this semicolon:
863+
x + 1;
864+
^
865+
error: not all control paths return a value
866+
fn add_one(x: int) -> int {
867+
x + 1;
868+
}
869+
```
870+
871+
Remember our earlier discussions about semicolons and `()`? Our function claims
872+
to return an `int`, but with a semicolon, it would return `()` instead. Rust
873+
realizes this probably isn't what we want, and suggests removing the semicolon.
874+
875+
This is very much like our `if` statement before: the result of the block
876+
(`{}`) is the value of the expression. Other expression-oriented languages,
877+
such as Ruby, work like this, but it's a bit unusual in the systems programming
878+
world. When people first learn about this, they usually assume that it
879+
introduces bugs. But because Rust's type system is so strong, and because unit
880+
is its own unique type, we have never seen an issue where adding or removing a
881+
semicolon in a return position would cause a bug.
882+
883+
But what about early returns? Rust does have a keyword for that, `return`:
884+
885+
```{rust}
886+
fn foo(x: int) -> int {
887+
if x < 5 { return x; }
888+
889+
x + 1
890+
}
891+
```
892+
893+
Using a `return` as the last line of a function works, but is considered poor
894+
style:
895+
896+
```{rust}
897+
fn foo(x: int) -> int {
898+
if x < 5 { return x; }
899+
900+
return x + 1;
901+
}
902+
```
903+
904+
There are some additional ways to define functions, but they involve features
905+
that we haven't learned about yet, so let's just leave it at that for now.
906+
907+
## Comments
908+
763909
return
764910

765911
comments

0 commit comments

Comments
 (0)