@@ -760,6 +760,152 @@ of Rust code. For that, we'll need our next concept: functions.
760
760
761
761
## Functions
762
762
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
+
763
909
return
764
910
765
911
comments
0 commit comments