Skip to content

Commit b355936

Browse files
committed
Convert ret to return
1 parent dc499f1 commit b355936

File tree

456 files changed

+3880
-3803
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

456 files changed

+3880
-3803
lines changed

doc/rust.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ if impl import
219219
let log loop
220220
mod mut
221221
pure
222-
ret
222+
return
223223
true trait type
224224
unchecked unsafe
225225
while
@@ -841,17 +841,17 @@ value has the corresponding [*function type*](#function-types), and can be
841841
used otherwise exactly as a function item (with a minor additional cost of
842842
calling the function indirectly).
843843

844-
Every control path in a function logically ends with a `ret` expression or a
844+
Every control path in a function logically ends with a `return` expression or a
845845
diverging expression. If the outermost block of a function has a
846846
value-producing expression in its final-expression position, that expression
847-
is interpreted as an implicit `ret` expression applied to the
847+
is interpreted as an implicit `return` expression applied to the
848848
final-expression.
849849

850850
An example of a function:
851851

852852
~~~~
853853
fn add(x: int, y: int) -> int {
854-
ret x + y;
854+
return x + y;
855855
}
856856
~~~~
857857

@@ -876,7 +876,7 @@ unifies with any type. Rust has no syntax for $\bot$.
876876

877877
It might be necessary to declare a diverging function because as mentioned
878878
previously, the typechecker checks that every control path in a function ends
879-
with a [`ret`](#return-expressions) or diverging expression. So, if `my_err`
879+
with a [`return`](#return-expressions) or diverging expression. So, if `my_err`
880880
were declared without the `!` annotation, the following code would not
881881
typecheck:
882882

@@ -885,7 +885,7 @@ typecheck:
885885
886886
fn f(i: int) -> int {
887887
if i == 42 {
888-
ret 42;
888+
return 42;
889889
}
890890
else {
891891
my_err(~"Bad number!");
@@ -895,7 +895,7 @@ fn f(i: int) -> int {
895895

896896
The typechecker would complain that `f` doesn't return a value in the
897897
`else` branch. Adding the `!` annotation on `my_err` would
898-
express that `f` requires no explicit `ret`, as if it returns
898+
express that `f` requires no explicit `return`, as if it returns
899899
control to the caller, it returns a value (true because it never returns
900900
control).
901901

@@ -915,7 +915,7 @@ An example of a predicate:
915915

916916
~~~~
917917
pure fn lt_42(x: int) -> bool {
918-
ret (x < 42);
918+
return (x < 42);
919919
}
920920
~~~~
921921

@@ -1845,7 +1845,7 @@ An example of an `as` expression:
18451845
fn avg(v: ~[float]) -> float {
18461846
let sum: float = sum(v);
18471847
let sz: float = len(v) as float;
1848-
ret sum / sz;
1848+
return sum / sz;
18491849
}
18501850
~~~~
18511851

@@ -2079,21 +2079,21 @@ For a block `b`, the expression `loop b` is semantically equivalent to
20792079
typestate analysis pass takes into account that `loop`s are infinite.
20802080

20812081
For example, the following (contrived) function uses a `loop` with a
2082-
`ret` expression:
2082+
`return` expression:
20832083

20842084
~~~~
20852085
fn count() -> bool {
20862086
let mut i = 0;
20872087
loop {
20882088
i += 1;
2089-
if i == 20 { ret true; }
2089+
if i == 20 { return true; }
20902090
}
20912091
}
20922092
~~~~
20932093

20942094
This function compiles, because typestate recognizes that the `loop`
2095-
never terminates (except non-locally, with `ret`), thus there is no
2096-
need to insert a spurious `fail` or `ret` after the `loop`. If `loop`
2095+
never terminates (except non-locally, with `return`), thus there is no
2096+
need to insert a spurious `fail` or `return` after the `loop`. If `loop`
20972097
were replaced with `while true`, the function would be rejected
20982098
because from the compiler's perspective, there would be a control path
20992099
along which `count` does not return a value (that is, if the loop
@@ -2200,7 +2200,7 @@ let x: list<int> = cons(10, @cons(11, @nil));
22002200
22012201
alt x {
22022202
cons(_, @nil) { fail ~"singleton list"; }
2203-
cons(*) { ret; }
2203+
cons(*) { return; }
22042204
nil { fail ~"empty list"; }
22052205
}
22062206
~~~~
@@ -2235,7 +2235,7 @@ alt x {
22352235
process_ten();
22362236
}
22372237
nil {
2238-
ret;
2238+
return;
22392239
}
22402240
_ {
22412241
fail;
@@ -2353,7 +2353,7 @@ fn read_file_lines(path: ~str) -> ~[~str] {
23532353
lines(f) |s| {
23542354
r += ~[s];
23552355
}
2356-
ret r;
2356+
return r;
23572357
}
23582358
~~~~
23592359

@@ -2372,23 +2372,23 @@ expression.
23722372
### Return expressions
23732373

23742374
~~~~~~~~{.ebnf .gram}
2375-
ret_expr : "ret" expr ? ;
2375+
return_expr : "return" expr ? ;
23762376
~~~~~~~~
23772377

2378-
Return expressions are denoted with the keyword `ret`. Evaluating a `ret`
2379-
expression^[A `ret` expression is analogous to a `return` expression
2378+
Return expressions are denoted with the keyword `return`. Evaluating a `return`
2379+
expression^[A `return` expression is analogous to a `return` expression
23802380
in the C family.] moves its argument into the output slot of the current
23812381
function, destroys the current function activation frame, and transfers
23822382
control to the caller frame.
23832383

2384-
An example of a `ret` expression:
2384+
An example of a `return` expression:
23852385

23862386
~~~~
23872387
fn max(a: int, b: int) -> int {
23882388
if a > b {
2389-
ret a;
2389+
return a;
23902390
}
2391-
ret b;
2391+
return b;
23922392
}
23932393
~~~~
23942394

@@ -2738,7 +2738,7 @@ An example of a `fn` type:
27382738

27392739
~~~~~~~~
27402740
fn add(x: int, y: int) -> int {
2741-
ret x + y;
2741+
return x + y;
27422742
}
27432743
27442744
let mut x = add(5,7);
@@ -2784,10 +2784,10 @@ Within the body of an item that has type parameter declarations, the names of it
27842784

27852785
~~~~~~~
27862786
fn map<A: copy, B: copy>(f: fn(A) -> B, xs: ~[A]) -> ~[B] {
2787-
if xs.len() == 0 { ret ~[]; }
2787+
if xs.len() == 0 { return ~[]; }
27882788
let first: B = f(xs[0]);
27892789
let rest: ~[B] = map(f, xs.slice(1, xs.len()));
2790-
ret ~[first] + rest;
2790+
return ~[first] + rest;
27912791
}
27922792
~~~~~~~
27932793

doc/tutorial.md

+24-26
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,15 @@ fn boring_old_factorial(n: int) -> int {
5454
result *= i;
5555
i += 1;
5656
}
57-
ret result;
57+
return result;
5858
}
5959
~~~~
6060

6161
Several differences from C stand out. Types do not come before, but
6262
after variable names (preceded by a colon). For local variables
6363
(introduced with `let`), types are optional, and will be inferred when
6464
left off. Constructs like `while` and `if` do not require parentheses
65-
around the condition (though they allow them). Also, there's a
66-
tendency towards aggressive abbreviation in the keywords—`fn` for
67-
function, `ret` for return.
65+
around the condition (though they allow them).
6866

6967
You should, however, not conclude that Rust is simply an evolution of
7068
C. As will become clear in the rest of this tutorial, it goes in quite
@@ -697,15 +695,15 @@ end of the block:
697695
fn signum(x: int) -> int {
698696
if x < 0 { -1 }
699697
else if x > 0 { 1 }
700-
else { ret 0; }
698+
else { return 0; }
701699
}
702700
~~~~
703701

704-
The `ret` (return) and its semicolon could have been left out without
702+
The `return` and its semicolon could have been left out without
705703
changing the meaning of this function, but it illustrates that you
706704
will not get a type error in this case, although the last arm doesn't
707705
have type `int`, because control doesn't reach the end of that arm
708-
(`ret` is jumping out of the function).
706+
(`return` is jumping out of the function).
709707

710708
## Pattern matching
711709

@@ -913,11 +911,11 @@ colons and the return type follows the arrow.
913911

914912
~~~~
915913
fn int_to_str(i: int) -> ~str {
916-
ret ~"tube sock";
914+
return ~"tube sock";
917915
}
918916
~~~~
919917

920-
The `ret` keyword immediately returns from the body of a function. It
918+
The `return` keyword immediately returns from the body of a function. It
921919
is optionally followed by an expression to return. A function can
922920
also return a value by having its top level block produce an
923921
expression.
@@ -926,9 +924,9 @@ expression.
926924
# const copernicus: int = 0;
927925
fn int_to_str(i: int) -> ~str {
928926
if i == copernicus {
929-
ret ~"tube sock";
927+
return ~"tube sock";
930928
} else {
931-
ret ~"violin";
929+
return ~"violin";
932930
}
933931
}
934932
~~~~
@@ -946,7 +944,7 @@ and both the return type and the return value may be omitted from
946944
the definition. The following two functions are equivalent.
947945

948946
~~~~
949-
fn do_nothing_the_hard_way() -> () { ret (); }
947+
fn do_nothing_the_hard_way() -> () { return (); }
950948
951949
fn do_nothing_the_easy_way() { }
952950
~~~~
@@ -1552,7 +1550,7 @@ their environment". For example you couldn't write the following:
15521550
let foo = 10;
15531551
15541552
fn bar() -> int {
1555-
ret foo; // `bar` cannot refer to `foo`
1553+
return foo; // `bar` cannot refer to `foo`
15561554
}
15571555
~~~~
15581556

@@ -1617,7 +1615,7 @@ returns it from a function, and then calls it:
16171615
use std;
16181616
16191617
fn mk_appender(suffix: ~str) -> fn@(~str) -> ~str {
1620-
ret fn@(s: ~str) -> ~str { s + suffix };
1618+
return fn@(s: ~str) -> ~str { s + suffix };
16211619
}
16221620
16231621
fn main() {
@@ -1635,7 +1633,7 @@ be written:
16351633

16361634
~~~~
16371635
fn mk_appender(suffix: ~str) -> fn@(~str) -> ~str {
1638-
ret |s| s + suffix;
1636+
return |s| s + suffix;
16391637
}
16401638
~~~~
16411639

@@ -1742,7 +1740,7 @@ Empty argument lists can be omitted from `do` expressions.
17421740

17431741
Most iteration in Rust is done with `for` loops. Like `do`,
17441742
`for` is a nice syntax for doing control flow with closures.
1745-
Additionally, within a `for` loop, `break`, `again`, and `ret`
1743+
Additionally, within a `for` loop, `break`, `again`, and `retern`
17461744
work just as they do with `while` and `loop`.
17471745

17481746
Consider again our `each` function, this time improved to
@@ -1790,7 +1788,7 @@ for each(~[2, 4, 8, 5, 16]) |n| {
17901788
}
17911789
~~~~
17921790

1793-
As an added bonus, you can use the `ret` keyword, which is not
1791+
As an added bonus, you can use the `return` keyword, which is not
17941792
normally allowed in closures, in a block that appears as the body of a
17951793
`for` loop — this will cause a return to happen from the outer
17961794
function, not just the loop body.
@@ -1799,7 +1797,7 @@ function, not just the loop body.
17991797
# import each = vec::each;
18001798
fn contains(v: ~[int], elt: int) -> bool {
18011799
for each(v) |x| {
1802-
if (x == elt) { ret true; }
1800+
if (x == elt) { return true; }
18031801
}
18041802
false
18051803
}
@@ -1960,7 +1958,7 @@ copy, that's a win.
19601958
~~~~
19611959
type person = {name: ~str, address: ~str};
19621960
fn make_person(+name: ~str, +address: ~str) -> person {
1963-
ret {name: name, address: address};
1961+
return {name: name, address: address};
19641962
}
19651963
~~~~
19661964

@@ -1987,7 +1985,7 @@ fn map<T, U>(vector: ~[T], function: fn(T) -> U) -> ~[U] {
19871985
for vector.each |element| {
19881986
vec::push(accumulator, function(element));
19891987
}
1990-
ret accumulator;
1988+
return accumulator;
19911989
}
19921990
~~~~
19931991

@@ -2473,7 +2471,7 @@ fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
24732471
else { result += ~", "; }
24742472
result += elt.to_str();
24752473
}
2476-
ret result;
2474+
return result;
24772475
}
24782476
~~~~
24792477

@@ -2633,14 +2631,14 @@ extern mod crypto {
26332631
fn as_hex(data: ~[u8]) -> ~str {
26342632
let mut acc = ~"";
26352633
for data.each |byte| { acc += #fmt("%02x", byte as uint); }
2636-
ret acc;
2634+
return acc;
26372635
}
26382636
26392637
fn sha1(data: ~str) -> ~str unsafe {
26402638
let bytes = str::bytes(data);
26412639
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
26422640
vec::len(bytes) as c_uint, ptr::null());
2643-
ret as_hex(vec::unsafe::from_buf(hash, 20u));
2641+
return as_hex(vec::unsafe::from_buf(hash, 20u));
26442642
}
26452643
26462644
fn main(args: ~[~str]) {
@@ -2740,7 +2738,7 @@ fn sha1(data: ~str) -> ~str {
27402738
let bytes = str::bytes(data);
27412739
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
27422740
vec::len(bytes), ptr::null());
2743-
ret as_hex(vec::unsafe::from_buf(hash, 20u));
2741+
return as_hex(vec::unsafe::from_buf(hash, 20u));
27442742
}
27452743
}
27462744
~~~~
@@ -2783,7 +2781,7 @@ Let's look at our `sha1` function again.
27832781
let bytes = str::bytes(data);
27842782
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
27852783
vec::len(bytes), ptr::null());
2786-
ret as_hex(vec::unsafe::from_buf(hash, 20u));
2784+
return as_hex(vec::unsafe::from_buf(hash, 20u));
27872785
# }
27882786
# }
27892787
~~~~
@@ -2830,7 +2828,7 @@ extern mod lib_c {
28302828
fn unix_time_in_microseconds() -> u64 unsafe {
28312829
let x = {mut tv_sec: 0 as c_ulonglong, mut tv_usec: 0 as c_ulonglong};
28322830
lib_c::gettimeofday(ptr::addr_of(x), ptr::null());
2833-
ret (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
2831+
return (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
28342832
}
28352833
28362834
# fn main() { assert #fmt("%?", unix_time_in_microseconds()) != ~""; }

0 commit comments

Comments
 (0)