Permalink
Browse files

Convert ret to return

  • Loading branch information...
1 parent dc499f1 commit b355936b4da0831f47afe8f251daee503c8caa32 @brson brson committed Aug 2, 2012
Showing 456 changed files with 3,880 additions and 3,803 deletions.
View
@@ -219,7 +219,7 @@ if impl import
let log loop
mod mut
pure
-ret
+return
true trait type
unchecked unsafe
while
@@ -841,17 +841,17 @@ value has the corresponding [*function type*](#function-types), and can be
used otherwise exactly as a function item (with a minor additional cost of
calling the function indirectly).
-Every control path in a function logically ends with a `ret` expression or a
+Every control path in a function logically ends with a `return` expression or a
diverging expression. If the outermost block of a function has a
value-producing expression in its final-expression position, that expression
-is interpreted as an implicit `ret` expression applied to the
+is interpreted as an implicit `return` expression applied to the
final-expression.
An example of a function:
~~~~
fn add(x: int, y: int) -> int {
- ret x + y;
+ return x + y;
}
~~~~
@@ -876,7 +876,7 @@ unifies with any type. Rust has no syntax for $\bot$.
It might be necessary to declare a diverging function because as mentioned
previously, the typechecker checks that every control path in a function ends
-with a [`ret`](#return-expressions) or diverging expression. So, if `my_err`
+with a [`return`](#return-expressions) or diverging expression. So, if `my_err`
were declared without the `!` annotation, the following code would not
typecheck:
@@ -885,7 +885,7 @@ typecheck:
fn f(i: int) -> int {
if i == 42 {
- ret 42;
+ return 42;
}
else {
my_err(~"Bad number!");
@@ -895,7 +895,7 @@ fn f(i: int) -> int {
The typechecker would complain that `f` doesn't return a value in the
`else` branch. Adding the `!` annotation on `my_err` would
-express that `f` requires no explicit `ret`, as if it returns
+express that `f` requires no explicit `return`, as if it returns
control to the caller, it returns a value (true because it never returns
control).
@@ -915,7 +915,7 @@ An example of a predicate:
~~~~
pure fn lt_42(x: int) -> bool {
- ret (x < 42);
+ return (x < 42);
}
~~~~
@@ -1845,7 +1845,7 @@ An example of an `as` expression:
fn avg(v: ~[float]) -> float {
let sum: float = sum(v);
let sz: float = len(v) as float;
- ret sum / sz;
+ return sum / sz;
}
~~~~
@@ -2079,21 +2079,21 @@ For a block `b`, the expression `loop b` is semantically equivalent to
typestate analysis pass takes into account that `loop`s are infinite.
For example, the following (contrived) function uses a `loop` with a
-`ret` expression:
+`return` expression:
~~~~
fn count() -> bool {
let mut i = 0;
loop {
i += 1;
- if i == 20 { ret true; }
+ if i == 20 { return true; }
}
}
~~~~
This function compiles, because typestate recognizes that the `loop`
-never terminates (except non-locally, with `ret`), thus there is no
-need to insert a spurious `fail` or `ret` after the `loop`. If `loop`
+never terminates (except non-locally, with `return`), thus there is no
+need to insert a spurious `fail` or `return` after the `loop`. If `loop`
were replaced with `while true`, the function would be rejected
because from the compiler's perspective, there would be a control path
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));
alt x {
cons(_, @nil) { fail ~"singleton list"; }
- cons(*) { ret; }
+ cons(*) { return; }
nil { fail ~"empty list"; }
}
~~~~
@@ -2235,7 +2235,7 @@ alt x {
process_ten();
}
nil {
- ret;
+ return;
}
_ {
fail;
@@ -2353,7 +2353,7 @@ fn read_file_lines(path: ~str) -> ~[~str] {
lines(f) |s| {
r += ~[s];
}
- ret r;
+ return r;
}
~~~~
@@ -2372,23 +2372,23 @@ expression.
### Return expressions
~~~~~~~~{.ebnf .gram}
-ret_expr : "ret" expr ? ;
+return_expr : "return" expr ? ;
~~~~~~~~
-Return expressions are denoted with the keyword `ret`. Evaluating a `ret`
-expression^[A `ret` expression is analogous to a `return` expression
+Return expressions are denoted with the keyword `return`. Evaluating a `return`
+expression^[A `return` expression is analogous to a `return` expression
in the C family.] moves its argument into the output slot of the current
function, destroys the current function activation frame, and transfers
control to the caller frame.
-An example of a `ret` expression:
+An example of a `return` expression:
~~~~
fn max(a: int, b: int) -> int {
if a > b {
- ret a;
+ return a;
}
- ret b;
+ return b;
}
~~~~
@@ -2738,7 +2738,7 @@ An example of a `fn` type:
~~~~~~~~
fn add(x: int, y: int) -> int {
- ret x + y;
+ return x + y;
}
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
~~~~~~~
fn map<A: copy, B: copy>(f: fn(A) -> B, xs: ~[A]) -> ~[B] {
- if xs.len() == 0 { ret ~[]; }
+ if xs.len() == 0 { return ~[]; }
let first: B = f(xs[0]);
let rest: ~[B] = map(f, xs.slice(1, xs.len()));
- ret ~[first] + rest;
+ return ~[first] + rest;
}
~~~~~~~
View
@@ -54,17 +54,15 @@ fn boring_old_factorial(n: int) -> int {
result *= i;
i += 1;
}
- ret result;
+ return result;
}
~~~~
Several differences from C stand out. Types do not come before, but
after variable names (preceded by a colon). For local variables
(introduced with `let`), types are optional, and will be inferred when
left off. Constructs like `while` and `if` do not require parentheses
-around the condition (though they allow them). Also, there's a
-tendency towards aggressive abbreviation in the keywords—`fn` for
-function, `ret` for return.
+around the condition (though they allow them).
You should, however, not conclude that Rust is simply an evolution of
C. As will become clear in the rest of this tutorial, it goes in quite
@@ -697,15 +695,15 @@ end of the block:
fn signum(x: int) -> int {
if x < 0 { -1 }
else if x > 0 { 1 }
- else { ret 0; }
+ else { return 0; }
}
~~~~
-The `ret` (return) and its semicolon could have been left out without
+The `return` and its semicolon could have been left out without
changing the meaning of this function, but it illustrates that you
will not get a type error in this case, although the last arm doesn't
have type `int`, because control doesn't reach the end of that arm
-(`ret` is jumping out of the function).
+(`return` is jumping out of the function).
## Pattern matching
@@ -913,11 +911,11 @@ colons and the return type follows the arrow.
~~~~
fn int_to_str(i: int) -> ~str {
- ret ~"tube sock";
+ return ~"tube sock";
}
~~~~
-The `ret` keyword immediately returns from the body of a function. It
+The `return` keyword immediately returns from the body of a function. It
is optionally followed by an expression to return. A function can
also return a value by having its top level block produce an
expression.
@@ -926,9 +924,9 @@ expression.
# const copernicus: int = 0;
fn int_to_str(i: int) -> ~str {
if i == copernicus {
- ret ~"tube sock";
+ return ~"tube sock";
} else {
- ret ~"violin";
+ return ~"violin";
}
}
~~~~
@@ -946,7 +944,7 @@ and both the return type and the return value may be omitted from
the definition. The following two functions are equivalent.
~~~~
-fn do_nothing_the_hard_way() -> () { ret (); }
+fn do_nothing_the_hard_way() -> () { return (); }
fn do_nothing_the_easy_way() { }
~~~~
@@ -1552,7 +1550,7 @@ their environment". For example you couldn't write the following:
let foo = 10;
fn bar() -> int {
- ret foo; // `bar` cannot refer to `foo`
+ return foo; // `bar` cannot refer to `foo`
}
~~~~
@@ -1617,7 +1615,7 @@ returns it from a function, and then calls it:
use std;
fn mk_appender(suffix: ~str) -> fn@(~str) -> ~str {
- ret fn@(s: ~str) -> ~str { s + suffix };
+ return fn@(s: ~str) -> ~str { s + suffix };
}
fn main() {
@@ -1635,7 +1633,7 @@ be written:
~~~~
fn mk_appender(suffix: ~str) -> fn@(~str) -> ~str {
- ret |s| s + suffix;
+ return |s| s + suffix;
}
~~~~
@@ -1742,7 +1740,7 @@ Empty argument lists can be omitted from `do` expressions.
Most iteration in Rust is done with `for` loops. Like `do`,
`for` is a nice syntax for doing control flow with closures.
-Additionally, within a `for` loop, `break`, `again`, and `ret`
+Additionally, within a `for` loop, `break`, `again`, and `retern`
work just as they do with `while` and `loop`.
Consider again our `each` function, this time improved to
@@ -1790,7 +1788,7 @@ for each(~[2, 4, 8, 5, 16]) |n| {
}
~~~~
-As an added bonus, you can use the `ret` keyword, which is not
+As an added bonus, you can use the `return` keyword, which is not
normally allowed in closures, in a block that appears as the body of a
`for` loop — this will cause a return to happen from the outer
function, not just the loop body.
@@ -1799,7 +1797,7 @@ function, not just the loop body.
# import each = vec::each;
fn contains(v: ~[int], elt: int) -> bool {
for each(v) |x| {
- if (x == elt) { ret true; }
+ if (x == elt) { return true; }
}
false
}
@@ -1960,7 +1958,7 @@ copy, that's a win.
~~~~
type person = {name: ~str, address: ~str};
fn make_person(+name: ~str, +address: ~str) -> person {
- ret {name: name, address: address};
+ return {name: name, address: address};
}
~~~~
@@ -1987,7 +1985,7 @@ fn map<T, U>(vector: ~[T], function: fn(T) -> U) -> ~[U] {
for vector.each |element| {
vec::push(accumulator, function(element));
}
- ret accumulator;
+ return accumulator;
}
~~~~
@@ -2473,7 +2471,7 @@ fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
else { result += ~", "; }
result += elt.to_str();
}
- ret result;
+ return result;
}
~~~~
@@ -2633,14 +2631,14 @@ extern mod crypto {
fn as_hex(data: ~[u8]) -> ~str {
let mut acc = ~"";
for data.each |byte| { acc += #fmt("%02x", byte as uint); }
- ret acc;
+ return acc;
}
fn sha1(data: ~str) -> ~str unsafe {
let bytes = str::bytes(data);
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
vec::len(bytes) as c_uint, ptr::null());
- ret as_hex(vec::unsafe::from_buf(hash, 20u));
+ return as_hex(vec::unsafe::from_buf(hash, 20u));
}
fn main(args: ~[~str]) {
@@ -2740,7 +2738,7 @@ fn sha1(data: ~str) -> ~str {
let bytes = str::bytes(data);
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
vec::len(bytes), ptr::null());
- ret as_hex(vec::unsafe::from_buf(hash, 20u));
+ return as_hex(vec::unsafe::from_buf(hash, 20u));
}
}
~~~~
@@ -2783,7 +2781,7 @@ Let's look at our `sha1` function again.
let bytes = str::bytes(data);
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
vec::len(bytes), ptr::null());
-ret as_hex(vec::unsafe::from_buf(hash, 20u));
+return as_hex(vec::unsafe::from_buf(hash, 20u));
# }
# }
~~~~
@@ -2830,7 +2828,7 @@ extern mod lib_c {
fn unix_time_in_microseconds() -> u64 unsafe {
let x = {mut tv_sec: 0 as c_ulonglong, mut tv_usec: 0 as c_ulonglong};
lib_c::gettimeofday(ptr::addr_of(x), ptr::null());
- ret (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
+ return (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
}
# fn main() { assert #fmt("%?", unix_time_in_microseconds()) != ~""; }
Oops, something went wrong.

0 comments on commit b355936

Please sign in to comment.