for elt: T in seq { f(elt); }
}
fn map<T, U>(seq: [T], f: fn(T) -> U) -> [U] {
- let acc = [];
+ let mut acc = [];
for elt in seq { acc += [f(elt)]; }
acc
}
cat
}
-let a: animal = dog;
+let mut a: animal = dog;
a = cat;
~~~~
~~~~
impl uint_loops for uint {
fn times(f: fn(uint)) {
- let i = 0u;
+ let mut i = 0u;
while i < self { f(i); i += 1u; }
}
}
An example of three different move expressions:
~~~~~~~~
-# let x = [mut 0];
+# let mut x = [mut 0];
# let a = [mut 0];
# let b = 0;
# let y = {mut z: 0};
An example of three different swap expressions:
~~~~~~~~
-# let x = [mut 0];
-# let a = [mut 0];
+# let mut x = [mut 0];
+# let mut a = [mut 0];
# let i = 0;
# let y = {mut z: 0};
# let b = {mut c: 0};
expressions have the same effect:
~~~~
-# let x = 0;
+# let mut x = 0;
# let y = 0;
x = y;
An example of a simple `while` expression:
~~~~
-# let i = 0;
+# let mut i = 0;
# let println = io::println;
while i < 10 {
An example of a `do`-`while` expression:
~~~~
-# let i = 0;
+# let mut i = 0;
# let println = io::println;
do {
~~~~
fn count() -> bool {
- let i = 0;
+ let mut i = 0;
loop {
i += 1;
if i == 20 { ret true; }
ret x + y;
}
-let x = add(5,7);
+let mut x = add(5,7);
type binop = fn(int,int) -> int;
let bo: binop = add;
For example, this code:
~~~~~~~~
-# let s;
+# let mut s;
s = "hello, world";
io::println(s);
value allocated in another frame *or* a boxed value in the heap. The
reference-formation rules ensure that the referent will outlive the reference.
-Local variables are always implicitly mutable.
+Local variables are immutable unless declared with `let mut`. The
+`mut` keyword applies to all local variables declared within that
+declaration (so `let mut x, y` declares two mutable variables, `x` and
+`y`).
Local variables are not initialized when allocated; the entire frame worth of
local variables are allocated at once, on frame-entry, in an uninitialized
~~~~
fn fac(n: int) -> int {
- let result = 1, i = 1;
+ let mut result = 1, i = 1;
while i <= n {
result *= i;
i += 1;
## Variable declaration
-The `let` keyword, as we've seen, introduces a local variable. Global
-constants can be defined with `const`:
+The `let` keyword, as we've seen, introduces a local variable. Local
+variables are immutable by default: `let mut` can be used to introduce
+a local variable that can be reassigned. Global constants can be
+defined with `const`:
~~~~
use std;
const repeat: uint = 5u;
fn main() {
- let count = 0u;
+ let hi = "Hi!";
+ let mut count = 0u;
while count < repeat {
- io::println("Hi!");
+ io::println(hi);
count += 1u;
}
}
~~~~
// The type of this vector will be inferred based on its use.
let x = [];
-# x = [3];
+# vec::map(x, fn&(&&_y:int) -> int { _y });
// Explicitly say this is a vector of integers.
let y: [int] = [];
~~~~
to abort the current iteration and continue with the next.
~~~~
-let x = 5;
+let mut x = 5;
while true {
x += x - 3;
if x % 5 == 0 { break; }
*should* hold at a certain point in a program.
~~~~
-let x = 100;
+let mut x = 100;
while (x > 10) { x -= 10; }
assert x == 10;
~~~~
~~~~
fn for_rev(v: [int], act: fn(int)) {
- let i = vec::len(v);
+ let mut i = vec::len(v);
while (i > 0u) {
i -= 1u;
act(v[i]);
Growing a vector in Rust is not as inefficient as it looks :
~~~~
-let myvec = [], i = 0;
+let mut myvec = [], i = 0;
while i < 100 {
myvec += [i];
i += 1;
~~~~
# fn myfunc(a: int, b: fn()) {}
# fn get_another_record() -> int { 1 }
-# let x = 1;
+# let mut x = 1;
myfunc(x, {|| x = get_another_record(); });
~~~~
safety.
~~~~
-let my_rec = {a: 4, b: [1, 2, 3]};
+let mut my_rec = {a: 4, b: [1, 2, 3]};
alt my_rec {
{a, b} {
log(info, b); // This is okay
~~~~
fn for_rev<T>(v: [T], act: fn(T)) {
- let i = vec::len(v);
+ let mut i = vec::len(v);
while i > 0u {
i -= 1u;
act(v[i]);
}
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] {
- let acc = [];
+ let mut acc = [];
for elt in v { acc += [f(elt)]; }
ret acc;
}
~~~~
let n = option::none;
-# n = option::some(1);
+# option::may(n, fn&(&&x:int) {})
~~~~
If you never do anything else with `n`, the compiler will not be able
~~~~
# iface to_str { fn to_str() -> str; }
fn comma_sep<T: to_str>(elts: [T]) -> str {
- let result = "", first = true;
+ let mut result = "", first = true;
for elt in elts {
if first { first = false; }
else { result += ", "; }
# fn mk_currency(x: int, s: str) {}
impl int_util for int {
fn times(b: fn(int)) {
- let i = 0;
+ let mut i = 0;
while i < self { b(i); i += 1; }
}
fn dollars() -> currency {
~~~~
fn stringifier(from_parent: comm::port<uint>,
to_parent: comm::chan<str>) {
- let value: uint;
+ let mut value: uint;
do {
value = comm::recv(from_parent);
comm::send(to_parent, uint::to_str(value, 10u));
USE_SNAPSHOT_RUNTIME=0
USE_SNAPSHOT_CORELIB=0
-# Do not use --enforce-mut-vars in stage0, for now, as the snapshot
-# has an older version of the check.
-ENFORCE_MUT_VARS_0=
-ENFORCE_MUT_VARS_1=--enforce-mut-vars
-ENFORCE_MUT_VARS_2=--enforce-mut-vars
-ENFORCE_MUT_VARS_3=--enforce-mut-vars
-
define TARGET_STAGE_N
$$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.ll: \
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB) \
$$(TSREQ$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
- $$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
- -o $$@ $$< && touch $$@
+ $$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUSTLLVM): \
rustllvm/$(2)/$$(CFG_RUSTLLVM)
$$(RUSTC_INPUTS) \
$$(TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
- $$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
- -o $$@ $$<
+ $$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$<
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC): \
$$(COMPILER_CRATE) $$(COMPILER_INPUTS) \
$$(TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3)) \
$$(TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
- $$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
- -o $$@ $$< && touch $$@
+ $$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@
endef
$$(CORELIB_CRATE) $$(CORELIB_INPUTS) \
$$(TSREQ$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
- $$(STAGE$(1)_T_$(2)_H_$(3)) --enforce-mut-vars -o $$@ $$< && touch $$@
+ $$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@
endef
fn load_link(mis: [@ast::meta_item]) -> (option<str>,
option<str>,
option<str>) {
- let name = none;
- let vers = none;
- let uuid = none;
+ let mut name = none;
+ let mut vers = none;
+ let mut uuid = none;
for a: @ast::meta_item in mis {
alt a.node {
ast::meta_name_value(v, {node: ast::lit_str(s), span: _}) {
};
let c = parser::parse_crate_from_crate_file(filename, [], sess);
- let name = none;
- let vers = none;
- let uuid = none;
- let desc = none;
- let sigs = none;
- let crate_type = none;
+ let mut name = none;
+ let mut vers = none;
+ let mut uuid = none;
+ let mut desc = none;
+ let mut sigs = none;
+ let mut crate_type = none;
for a in c.node.attrs {
alt a.node.value.node {
_ { none }
};
- let tags = [];
+ let mut tags = [];
alt p.find("tags") {
some(json::list(js)) {
for j in js {
let sources = map::str_hash::<source>();
try_parse_sources(path::connect(syscargo, "sources.json"), sources);
try_parse_sources(path::connect(syscargo, "local-sources.json"), sources);
- let c = {
+ let mut c = {
pgp: pgp::supported(),
root: p,
bindir: path::connect(p, "bin"),
need_dir(c.bindir);
sources.keys { |k|
- let s = sources.get(k);
+ let mut s = sources.get(k);
load_source_packages(c, s);
sources.insert(k, s);
};
ret;
}
if !syncing {
- let npkg = 0u;
+ let mut npkg = 0u;
c.sources.values({ |v| npkg += vec::len(v.packages) });
if npkg == 0u {
error("No packages known. You may wish to run " +
}
fn install_uuid(c: cargo, wd: str, uuid: str) {
- let ps = [];
+ let mut ps = [];
for_each_package(c, { |s, p|
info(#fmt["%s ? %s", p.uuid, uuid]);
if p.uuid == uuid {
}
fn install_named(c: cargo, wd: str, name: str) {
- let ps = [];
+ let mut ps = [];
for_each_package(c, { |s, p|
if p.name == name {
vec::grow(ps, 1u, (s, p));
};
if str::starts_with(target, "uuid:") {
- let uuid = rest(target, 5u);
+ let mut uuid = rest(target, 5u);
alt str::find_char(uuid, '/') {
option::some(idx) {
let source = str::slice(uuid, 0u, idx);
}
}
} else {
- let name = target;
+ let mut name = target;
alt str::find_char(name, '/') {
option::some(idx) {
let source = str::slice(name, 0u, idx);
}
fn print_pkg(s: source, p: package) {
- let m = s.name + "/" + p.name + " (" + p.uuid + ")";
+ let mut m = s.name + "/" + p.name + " (" + p.uuid + ")";
if vec::len(p.tags) > 0u {
m = m + " [" + str::connect(p.tags, ", ") + "]";
}
cmd_usage();
ret;
}
- let n = 0;
+ let mut n = 0;
let name = c.opts.free[2];
let tags = vec::slice(c.opts.free, 3u, vec::len(c.opts.free));
for_each_package(c, { |s, p|
fn make_tests(config: config) -> [test::test_desc] {
#debug("making tests from %s", config.src_base);
- let tests = [];
+ let mut tests = [];
for file: str in os::list_dir(config.src_base) {
let file = file;
#debug("inspecting file %s", file);
let invalid_prefixes = [".", "#", "~"];
let name = path::basename(testfile);
- let valid = false;
+ let mut valid = false;
for ext in valid_extensions {
if str::ends_with(name, ext) { valid = true; }
// Load any test directives embedded in the file
fn load_errors(testfile: str) -> [expected_error] {
- let error_patterns = [];
+ let mut error_patterns = [];
let rdr = result::get(io::file_reader(testfile));
- let line_num = 1u;
+ let mut line_num = 1u;
while !rdr.eof() {
let ln = rdr.read_line();
error_patterns += parse_expected(line_num, ln);
fn parse_expected(line_num: uint, line: str) -> [expected_error] unsafe {
let error_tag = "//!";
- let idx;
+ let mut idx;
alt str::find_str(line, error_tag) {
option::none { ret []; }
option::some(nn) { idx = (nn as uint) + str::len(error_tag); }
// "//!^^^ kind msg" denotes a message expected
// three lines above current line:
- let adjust_line = 0u;
+ let mut adjust_line = 0u;
let len = str::len(line);
while idx < len && line[idx] == ('^' as u8) {
adjust_line += 1u;
// Load any test directives embedded in the file
fn load_props(testfile: str) -> test_props {
- let error_patterns = [];
- let aux_builds = [];
- let compile_flags = option::none;
- let pp_exact = option::none;
+ let mut error_patterns = [];
+ let mut aux_builds = [];
+ let mut compile_flags = option::none;
+ let mut pp_exact = option::none;
iter_header(testfile) {|ln|
alt parse_error_pattern(ln) {
option::some(ep) { error_patterns += [ep]; }
}
fn is_test_ignored(config: config, testfile: str) -> bool {
- let found = false;
+ let mut found = false;
iter_header(testfile) {|ln|
// FIXME: Can't return or break from iterator
// (Fix when Issue #1619 is resolved)
comm::send(ch, (1, output));
};
let status = run::waitpid(pid);
- let errs = "";
- let outs = "";
- let count = 2;
+ let mut errs = "";
+ let mut outs = "";
+ let mut count = 2;
while count > 0 {
let stream = comm::recv(p);
alt check stream {
// Copied from run::program_output
let file = os::fdopen(fd);
let reader = io::FILE_reader(file, false);
- let buf = "";
+ let mut buf = "";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::from_bytes(bytes);
}
fn run_rfail_test(config: config, props: test_props, testfile: str) {
- let procres = compile_test(config, props, testfile);
+ let mut procres = compile_test(config, props, testfile);
if procres.status != 0 { fatal_procres("compilation failed!", procres); }
}
fn run_rpass_test(config: config, props: test_props, testfile: str) {
- let procres = compile_test(config, props, testfile);
+ let mut procres = compile_test(config, props, testfile);
if procres.status != 0 { fatal_procres("compilation failed!", procres); }
let rounds =
alt props.pp_exact { option::some(_) { 1 } option::none { 2 } };
- let srcs = [result::get(io::read_whole_file_str(testfile))];
+ let mut srcs = [result::get(io::read_whole_file_str(testfile))];
- let round = 0;
+ let mut round = 0;
while round < rounds {
logv(config, #fmt["pretty-printing round %d", round]);
let procres = print_source(config, testfile, srcs[round]);
round += 1;
}
- let expected =
+ let mut expected =
alt props.pp_exact {
option::some(file) {
let filepath = path::connect(path::dirname(testfile), file);
}
option::none { srcs[vec::len(srcs) - 2u] }
};
- let actual = srcs[vec::len(srcs) - 1u];
+ let mut actual = srcs[vec::len(srcs) - 1u];
if option::is_some(props.pp_exact) {
// Now we have to care about line endings
fn make_typecheck_args(config: config, _testfile: str) -> procargs {
let prog = config.rustc_path;
- let args = ["-", "--no-trans", "--lib", "-L", config.build_base];
+ let mut args = ["-", "--no-trans", "--lib", "-L", config.build_base];
args += split_maybe_args(config.rustcflags);
ret {prog: prog, args: args};
}
fatal("process did not return an error status");
}
- let next_err_idx = 0u;
- let next_err_pat = props.error_patterns[next_err_idx];
+ let mut next_err_idx = 0u;
+ let mut next_err_pat = props.error_patterns[next_err_idx];
for line: str in str::split_char(procres.stderr, '\n') {
if str::contains(line, next_err_pat) {
#debug("found error pattern %s", next_err_pat);
// where line1:col1: is the starting point, line2:col2:
// is the ending point, and * represents ANSI color codes.
for line: str in str::split_char(procres.stderr, '\n') {
- let was_expected = false;
+ let mut was_expected = false;
vec::iteri(expected_errors) {|i, ee|
if !found_flags[i] {
#debug["prefix=%s ee.kind=%s ee.msg=%s line=%s",
xform: fn(config, str) -> str, testfile: str) ->
procargs {
let prog = config.rustc_path;
- let args = [testfile, "-o", xform(config, testfile),
- "-L", config.build_base] + extras;
+ let mut args = [testfile, "-o", xform(config, testfile),
+ "-L", config.build_base] + extras;
args += split_maybe_args(config.rustcflags);
args += split_maybe_args(props.compile_flags);
ret {prog: prog, args: args};
}
fn under(n: uint, it: fn(uint)) {
- let i: uint = 0u;
+ let mut i: uint = 0u;
while i < n { it(i); i += 1u; }
}
fn check_roundtrip_convergence(code: @str, maxIters: uint) {
- let i = 0u;
- let newv = code;
- let oldv = code;
+ let mut i = 0u;
+ let mut newv = code;
+ let mut oldv = code;
while i < maxIters {
oldv = newv;
#error("usage: %s <testdir>", args[0]);
ret;
}
- let files = [];
+ let mut files = [];
let root = args[1];
find_rust_files(files, root);
};
}
- let as = 0;
- let bs = 0;
+ let mut as = 0;
+ let mut bs = 0;
iter::repeat(msgs * times * 2u) {||
alt check select2(po_a, po_b) {
either::left("a") { as += 1 }
assert(vec::slice(ivals, 0u, vec::len(res)) ==
vec::map(res, {|x| x as int}));
}
- let i = 0u;
+ let mut i = 0u;
while i < 8u {
check_read_ln(i, wide_test, ivals);
i += 1u;
#[test]
fn test_flat_map_with_list() {
fn repeat(&&i: int) -> [int] {
- let r = [];
+ let mut r = [];
int::range(0, i) {|_j| r += [i]; }
r
}
#[test]
fn test_repeat() {
- let c = [],
- i = 0u;
+ let mut c = [], i = 0u;
repeat(5u) {||
c += [(i * i)];
i += 1u;
#[test]
#[ignore(reason = "fails periodically on mac")]
fn test_getenv_big() {
- let s = "";
- let i = 0;
+ let mut s = "";
+ let mut i = 0;
while i < 100 { s += "aaaaaaaaaa"; i += 1; }
let n = make_rand_name();
setenv(n, s);
fn test_env_setenv() {
let n = make_rand_name();
- let e = env();
+ let mut e = env();
setenv(n, "VALUE");
assert !vec::contains(e, (n, "VALUE"));
// Copied from run::program_output
let file = os::fdopen(fd);
let reader = io::FILE_reader(file, false);
- let buf = "";
+ let mut buf = "";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::from_bytes(bytes);
#[test]
fn test_pop_char() {
- let data = "ประเทศไทย中华";
+ let mut data = "ประเทศไทย中华";
let cc = pop_char(data);
assert "ประเทศไทย中" == data;
assert '华' == cc;
#[test]
fn test_pop_char_2() {
- let data2 = "华";
+ let mut data2 = "华";
let cc2 = pop_char(data2);
assert "" == data2;
assert '华' == cc2;
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_pop_char_fail() {
- let data = "";
+ let mut data = "";
let _cc3 = pop_char(data);
}
assert find_str_between(data, "ab", 2u, 6u) == some(3u);
assert find_str_between(data, "ab", 2u, 4u) == none;
- let data = "ประเทศไทย中华Việt Nam";
+ let mut data = "ประเทศไทย中华Việt Nam";
data += data;
assert find_str_between(data, "", 0u, 43u) == some(0u);
assert find_str_between(data, "", 6u, 43u) == some(6u);
assert (eq("bc", unsafe::slice_bytes("abc", 1u, 3u)));
assert (eq("", unsafe::slice_bytes("abc", 1u, 1u)));
fn a_million_letter_a() -> str {
- let i = 0;
- let rs = "";
+ let mut i = 0;
+ let mut rs = "";
while i < 100000 { rs += "aaaaaaaaaa"; i += 1; }
ret rs;
}
fn half_a_million_letter_a() -> str {
- let i = 0;
- let rs = "";
+ let mut i = 0;
+ let mut rs = "";
while i < 100000 { rs += "aaaaa"; i += 1; }
ret rs;
}
assert "华" == slice(data, 30u, 33u);
fn a_million_letter_X() -> str {
- let i = 0;
- let rs = "";
+ let mut i = 0;
+ let mut rs = "";
while i < 100000 { rs += "华华华华华华华华华华"; i += 1; }
ret rs;
}
fn half_a_million_letter_X() -> str {
- let i = 0;
- let rs = "";
+ let mut i = 0;
+ let mut rs = "";
while i < 100000 { rs += "华华华华华"; i += 1; }
ret rs;
}
#[test]
fn test_shift_byte() unsafe {
- let s = "ABC";
+ let mut s = "ABC";
let b = unsafe::shift_byte(s);
assert (s == "BC");
assert (b == 65u8);
#[test]
fn test_pop_byte() unsafe {
- let s = "ABC";
+ let mut s = "ABC";
let b = unsafe::pop_byte(s);
assert (s == "AB");
assert (b == 67u8);
let v: [u8] = bytes(s1);
let s2: str = from_bytes(v);
- let i: uint = 0u;
+ let mut i: uint = 0u;
let n1: uint = len(s1);
let n2: uint = vec::len::<u8>(v);
assert (n1 == n2);
#[test]
fn test_chars_iter() {
- let i = 0;
+ let mut i = 0;
chars_iter("x\u03c0y") {|ch|
alt check i {
0 { assert ch == 'x'; }
#[test]
fn test_bytes_iter() {
- let i = 0;
+ let mut i = 0;
bytes_iter("xyz") {|bb|
alt check i {
fn test_split_char_iter() {
let data = "\nMary had a little lamb\nLittle lamb\n";
- let ii = 0;
+ let mut ii = 0;
split_char_iter(data, ' ') {|xx|
alt ii {
fn test_splitn_char_iter() {
let data = "\nMary had a little lamb\nLittle lamb\n";
- let ii = 0;
+ let mut ii = 0;
splitn_char_iter(data, ' ', 2u) {|xx|
alt ii {
fn test_words_iter() {
let data = "\nMary had a little lamb\nLittle lamb\n";
- let ii = 0;
+ let mut ii = 0;
words_iter(data) {|ww|
alt ii {
fn test_lines_iter () {
let lf = "\nMary had a little lamb\nLittle lamb\n";
- let ii = 0;
+ let mut ii = 0;
lines_iter(lf) {|x|
alt ii {
comm::recv(start_po);
fn pingpong(po: comm::port<int>, ch: comm::chan<int>) {
- let val = 20;
+ let mut val = 20;
while val > 0 {
val = comm::recv(po);
comm::send(ch, val - 1);
fn test_unsafe_ptrs() unsafe {
// Test on-stack copy-from-buf.
let a = [1, 2, 3];
- let ptr = unsafe::to_ptr(a);
+ let mut ptr = unsafe::to_ptr(a);
let b = unsafe::from_buf(ptr, 3u);
assert (len(b) == 3u);
assert (b[0] == 1);
#[test]
fn test_from_fn() {
// Test on-stack from_fn.
- let v = from_fn(3u, square);
+ let mut v = from_fn(3u, square);
assert (len(v) == 3u);
assert (v[0] == 0u);
assert (v[1] == 1u);
#[test]
fn test_from_elem() {
// Test on-stack from_elem.
- let v = from_elem(2u, 10u);
+ let mut v = from_elem(2u, 10u);
assert (len(v) == 2u);
assert (v[0] == 10u);
assert (v[1] == 10u);
#[test]
fn test_tail() {
- let a = [11];
+ let mut a = [11];
assert (tail(a) == []);
a = [11, 12];
#[test]
fn test_last() {
- let n = last_opt([]);
+ let mut n = last_opt([]);
assert (n == none);
n = last_opt([1, 2, 3]);
assert (n == some(3));
#[test]
fn test_slice() {
// Test on-stack -> on-stack slice.
- let v = slice([1, 2, 3], 1u, 3u);
+ let mut v = slice([1, 2, 3], 1u, 3u);
assert (len(v) == 2u);
assert (v[0] == 2);
assert (v[1] == 3);
#[test]
fn test_pop() {
// Test on-stack pop.
- let v = [1, 2, 3];
- let e = pop(v);
+ let mut v = [1, 2, 3];
+ let mut e = pop(v);
assert (len(v) == 2u);
assert (v[0] == 1);
assert (v[1] == 2);
#[test]
fn test_push() {
// Test on-stack push().
- let v = [];
+ let mut v = [];
push(v, 1);
assert (len(v) == 1u);
assert (v[0] == 1);
#[test]
fn test_grow() {
// Test on-stack grow().
- let v = [];
+ let mut v = [];
grow(v, 2u, 1);
assert (len(v) == 2u);
assert (v[0] == 1);
#[test]
fn test_grow_fn() {
- let v = [];
+ let mut v = [];
grow_fn(v, 3u, square);
assert (len(v) == 3u);
assert (v[0] == 0u);
#[test]
fn test_grow_set() {
- let v = [mutable 1, 2, 3];
+ let mut v = [mutable 1, 2, 3];
grow_set(v, 4u, 4, 5);
assert (len(v) == 5u);
assert (v[0] == 1);
#[test]
fn test_map() {
// Test on-stack map.
- let v = [1u, 2u, 3u];
- let w = map(v, square_ref);
+ let mut v = [1u, 2u, 3u];
+ let mut w = map(v, square_ref);
assert (len(w) == 3u);
assert (w[0] == 1u);
assert (w[1] == 4u);
let v0 = [1, 2, 3, 4, 5];
let v1 = [5, 4, 3, 2, 1];
let u = map2::<int, int, int>(v0, v1, f);
- let i = 0;
+ let mut i = 0;
while i < 5 { assert (v0[i] * v1[i] == u[i]); i += 1; }
}
#[test]
fn test_filter_map() {
// Test on-stack filter-map.
- let v = [1u, 2u, 3u];
- let w = filter_map(v, square_if_odd);
+ let mut v = [1u, 2u, 3u];
+ let mut w = filter_map(v, square_if_odd);
assert (len(w) == 2u);
assert (w[0] == 1u);
assert (w[1] == 9u);
#[test]
fn test_foldl() {
// Test on-stack fold.
- let v = [1u, 2u, 3u];
- let sum = foldl(0u, v, add);
+ let mut v = [1u, 2u, 3u];
+ let mut sum = foldl(0u, v, add);
assert (sum == 6u);
// Test on-heap fold.
fn sub(&&a: int, &&b: int) -> int {
a - b
}
- let v = [1, 2, 3, 4];
+ let mut v = [1, 2, 3, 4];
let sum = foldl(0, v, sub);
assert sum == -10;
}
fn sub(&&a: int, &&b: int) -> int {
a - b
}
- let v = [1, 2, 3, 4];
+ let mut v = [1, 2, 3, 4];
let sum = foldr(v, 0, sub);
assert sum == -2;
}
#[test]
fn test_iter_empty() {
- let i = 0;
+ let mut i = 0;
iter::<int>([], { |_v| i += 1 });
assert i == 0;
}
#[test]
fn test_iter_nonempty() {
- let i = 0;
+ let mut i = 0;
iter([1, 2, 3], { |v| i += v });
assert i == 6;
}
#[test]
fn test_iteri() {
- let i = 0;
+ let mut i = 0;
iteri([1, 2, 3], { |j, v|
if i == 0 { assert v == 1; }
assert j + 1u == v as uint;
#[test]
fn test_riter_empty() {
- let i = 0;
+ let mut i = 0;
riter::<int>([], { |_v| i += 1 });
assert i == 0;
}
#[test]
fn test_riter_nonempty() {
- let i = 0;
+ let mut i = 0;
riter([1, 2, 3], { |v|
if i == 0 { assert v == 3; }
i += v
#[test]
fn test_riteri() {
- let i = 0;
+ let mut i = 0;
riteri([0, 1, 2], { |j, v|
if i == 0 { assert v == 2; }
assert j == v as uint;
#[test]
fn test_permute() {
- let results: [[int]];
+ let mut results: [[int]];
results = [];
permute([]) {|v| results += [v]; }
assert position_between([], 0u, 0u, f) == none;
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
- let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
+ let mut v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert position_between(v, 0u, 0u, f) == none;
assert position_between(v, 0u, 1u, f) == none;
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
fn g(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'd' }
- let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
+ let mut v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert find(v, f) == some((1, 'b'));
assert find(v, g) == none;
assert find_between([], 0u, 0u, f) == none;
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
- let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
+ let mut v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert find_between(v, 0u, 0u, f) == none;
assert find_between(v, 0u, 1u, f) == none;
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
fn g(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'd' }
- let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
+ let mut v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert position(v, f) == some(1u);
assert position(v, g) == none;
assert rposition_between([], 0u, 0u, f) == none;
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
- let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
+ let mut v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert rposition_between(v, 0u, 0u, f) == none;
assert rposition_between(v, 0u, 1u, f) == none;
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
fn g(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'd' }
- let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
+ let mut v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert rfind(v, f) == some((3, 'b'));
assert rfind(v, g) == none;
assert rfind_between([], 0u, 0u, f) == none;
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
- let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
+ let mut v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert rfind_between(v, 0u, 0u, f) == none;
assert rfind_between(v, 0u, 1u, f) == none;
#[test]
fn test_unshift() {
- let x = [1, 2, 3];
+ let mut x = [1, 2, 3];
unshift(x, 0);
assert x == [0, 1, 2, 3];
}
mod tests {
#[test]
fn test_0_elements() {
- let act;
- let exp;
+ let mut act;
+ let mut exp;
act = bitv(0u, false);
exp = vec::from_elem::<uint>(0u, 0u);
assert (eq_vec(act, exp));
#[test]
fn test_1_element() {
- let act;
+ let mut act;
act = bitv(1u, false);
assert (eq_vec(act, [0u]));
act = bitv(1u, true);
#[test]
fn test_10_elements() {
- let act;
+ let mut act;
// all 0
act = bitv(10u, false);
#[test]
fn test_31_elements() {
- let act;
+ let mut act;
// all 0
act = bitv(31u, false);
#[test]
fn test_32_elements() {
- let act;
+ let mut act;
// all 0
act = bitv(32u, false);
#[test]
fn test_33_elements() {
- let act;
+ let mut act;
// all 0
act = bitv(33u, false);
assert (d.peek_front() == 42);
log(debug, d.peek_back());
assert (d.peek_back() == 137);
- let i: int = d.pop_front();
+ let mut i: int = d.pop_front();
log(debug, i);
assert (i == 42);
i = d.pop_back();
let eqer_uint: map::eqfn<uint> = eq_uint;
let hm_uu: map::hashmap<uint, uint> =
map::hashmap::<uint, uint>(hasher_uint, eqer_uint);
- let i: uint = 0u;
+ let mut i: uint = 0u;
while i < num_to_insert {
assert (hm_uu.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
let eqer: map::eqfn<uint> = eq;
let hm: map::hashmap<uint, uint> =
map::hashmap::<uint, uint>(hasher, eqer);
- let i: uint = 0u;
+ let mut i: uint = 0u;
while i < num_to_insert {
assert (hm.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
#[test]
fn of_string2() {
let buf = @ mutable "1234567890";
- let i = 0;
+ let mut i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
assert char_len(r) == str::char_len(*sample);
assert rope_to_string(r) == *sample;
- let string_iter = 0u;
+ let mut string_iter = 0u;
let string_len = str::len(*sample);
let rope_iter = iterator::char::start(r);
- let equal = true;
- let pos = 0u;
+ let mut equal = true;
+ let mut pos = 0u;
while equal {
alt(node::char_iterator::next(rope_iter)) {
option::none {
#[test]
fn iter1() {
let buf = @ mutable "1234567890";
- let i = 0;
+ let mut i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
- let len = 0u;
+ let mut len = 0u;
let it = iterator::char::start(r);
loop {
alt(node::char_iterator::next(it)) {
fn bal1() {
let init = @ "1234567890";
let buf = @ mutable * init;
- let i = 0;
+ let mut i = 0;
while i < 8 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r1 = of_str(sample);
- let r2 = of_str(init);
+ let mut r2 = of_str(init);
i = 0;
while i < 8 { r2 = append_rope(r2, r2); i+= 1;}
#[test]
fn char_at1() {
//Generate a large rope
- let r = of_str(@ "123456789");
+ let mut r = of_str(@ "123456789");
uint::range(0u, 10u){|_i|
r = append_rope(r, r);
}
//Copy it in the slowest possible way
- let r2 = empty();
+ let mut r2 = empty();
uint::range(0u, char_len(r)){|i|
r2 = append_char(r2, char_at(r, i));
}
assert eq(r, r2);
- let r3 = empty();
+ let mut r3 = empty();
uint::range(0u, char_len(r)){|i|
r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u));
}
fn concat1() {
//Generate a reasonable rope
let chunk = of_str(@ "123456789");
- let r = empty();
+ let mut r = empty();
uint::range(0u, 10u){|_i|
r = append_rope(r, chunk);
}
type test = {input: str, output: [u8]};
fn a_million_letter_a() -> str {
- let i = 0;
- let rs = "";
+ let mut i = 0;
+ let mut rs = "";
while i < 100000 { rs += "aaaaaaaaaa"; i += 1; }
ret rs;
}
fn check_vec_eq(v0: [u8], v1: [u8]) {
assert (vec::len::<u8>(v0) == vec::len::<u8>(v1));
let len = vec::len::<u8>(v0);
- let i = 0u;
+ let mut i = 0u;
while i < len {
let a = v0[i];
let b = v1[i];
// Test that it works when accepting the message in pieces
for t: test in tests {
let len = str::len(t.input);
- let left = len;
+ let mut left = len;
while left > 0u {
let take = (left + 1u) / 2u;
sh.input_str(str::slice(t.input, len - left,
let f1 = lt;
let f2 = equal;
quick_sort3::<int>(f1, f2, v1);
- let i = 0u;
+ let mut i = 0u;
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
fn leual(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = leual;
quick_sort::<int>(f, v1);
- let i = 0u;
+ let mut i = 0u;
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
fn le(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = le;
let v3 = merge_sort::<int>(f, v1);
- let i = 0u;
+ let mut i = 0u;
while i < len {
log(debug, v3[i]);
assert (v3[i] == v2[i]);
let tests =
{
let testfn = fn~() { };
- let tests = [];
+ let mut tests = [];
for name: str in names {
let test = {name: name, fn: testfn, ignore: false,
should_fail: false};
let cfg = parse_cfgspecs(getopts::opt_strs(match, "cfg"));
let test = opt_present(match, "test");
let warn_unused_imports = opt_present(match, "warn-unused-imports");
- let enforce_mut_vars = opt_present(match, "enforce-mut-vars");
let sopts: @session::options =
@{crate_type: crate_type,
static: static,
parse_only: parse_only,
no_trans: no_trans,
no_asm_comments: no_asm_comments,
- warn_unused_imports: warn_unused_imports,
- enforce_mut_vars: enforce_mut_vars};
+ warn_unused_imports: warn_unused_imports};
ret sopts;
}
parse_only: bool,
no_trans: bool,
no_asm_comments: bool,
- warn_unused_imports: bool,
- enforce_mut_vars: bool};
+ warn_unused_imports: bool};
type crate_metadata = {name: str, data: [u8]};
}
fn make_crate(with_bin: bool, with_lib: bool) -> @ast::crate {
- let attrs = [];
+ let mut attrs = [];
if with_bin { attrs += [make_crate_type_attr("bin")]; }
if with_lib { attrs += [make_crate_type_attr("lib")]; }
@ast_util::respan(ast_util::dummy_sp(), {
// variables are assigned at most once. But this requires a new kind of
// propagation (def. not assigned), so I didn't do that.
def_local(_, false) if msg == msg_move_out { none }
- def_local(_, false) if cx.tcx.sess.opts.enforce_mut_vars {
+ def_local(_, false) {
some("immutable local variable")
}
space_if_not_bol(s);
ibox(s, indent_unit);
word_nbsp(s, "let");
+
+ // if any are mutable, all are mutable
+ if vec::any(locs) {|l| l.node.is_mutbl } {
+ assert vec::all(locs) {|l| l.node.is_mutbl };
+ word_nbsp(s, "mut");
+ }
+
fn print_local(s: ps, &&loc: @ast::local) {
ibox(s, indent_unit);
print_local_decl(s, loc);
ignore_errors
);
- let keep_going = true;
+ let mut keep_going = true;
while keep_going {
alt comm::recv(po) {
handle_request(f) {
parse_only: false,
no_trans: false,
no_asm_comments: false,
- warn_unused_imports: false,
- enforce_mut_vars: false
+ warn_unused_imports: false
};
let codemap = codemap::new_codemap();
}
fn first_sentence_(s: str) -> str {
- let dotcount = 0;
+ let mut dotcount = 0;
// The index of the character following a single dot. This allows
// Things like [0..1) to appear in the brief description
let idx = str::find(s) {|ch|
fn paragraphs(s: str) -> [str] {
let lines = str::lines_any(s);
- let whitespace_lines = 0;
- let accum = "";
+ let mut whitespace_lines = 0;
+ let mut accum = "";
let paras = vec::foldl([], lines) {|paras, line|
- let res = paras;
+ let mut res = paras;
if str::is_whitespace(line) {
whitespace_lines += 1;
// Copied from run::program_output
let file = os::fdopen(fd);
let reader = io::FILE_reader(file, false);
- let buf = "";
+ let mut buf = "";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::from_bytes(bytes);
fn generic_writer(process: fn~(markdown: str)) -> writer {
let ch = task::spawn_listener {|po: comm::port<writeinstr>|
- let markdown = "";
- let keep_going = true;
+ let mut markdown = "";
+ let mut keep_going = true;
while keep_going {
alt comm::recv(po) {
write(s) { markdown += s; }
comm::send(chan, copy instr);
};
let future = future::from_fn {||
- let res = "";
+ let mut res = "";
loop {
alt comm::recv(port) {
write(s) { res += s }
map: map::hashmap<K, V>
) -> [(K, V)] {
- let vec = [];
+ let mut vec = [];
map.items {|k, v|
vec += [(k, v)];
}
};
let modpath = ast_map::path_to_str(vec::init(*path));
- let reexportdocs = [];
+ let mut reexportdocs = [];
for def in defs {
if !def.reexp { cont; }
alt def_map.find(def.id) {
original through each pass"
)];
- let passno = 0;
+ let mut passno = 0;
vec::foldl(doc, passes) {|doc, pass|
log(debug, #fmt("pass #%d", passno));
passno += 1;
let lines = str::lines(option::get(desc));
- let new_desc = none;
- let current_section = none;
- let sections = [];
+ let mut new_desc = none;
+ let mut current_section = none;
+ let mut sections = [];
for line in lines {
alt parse_header(line) {
fn unindent(s: str) -> str {
let lines = str::lines_any(s);
- let saw_first_line = false;
- let saw_second_line = false;
+ let mut saw_first_line = false;
+ let mut saw_second_line = false;
let min_indent = vec::foldl(uint::max_value, lines) {|min_indent, line|
// After we see the first non-whitespace line, look at
min_indent
} else {
saw_first_line = true;
- let spaces = 0u;
+ let mut spaces = 0u;
str::all(line) {|char|
// Only comparing against space because I wouldn't
// know what to do with mixed whitespace chars
impl helpers for uint {
#[inline]
fn to(v: uint, f: fn(uint)) {
- let i = self;
+ let mut i = self;
while i < v {
f(i);
i += 1u;
#[inline]
fn iter<T>(v: [T], f: fn(T)) {
- let i = 0u;
+ let mut i = 0u;
let n = vec::len(v);
while i < n {
f(v[i]);
// same as cci_iter_lib, more-or-less, but not marked inline
fn iter(v: [uint], f: fn(uint)) {
- let i = 0u;
+ let mut i = 0u;
let n = vec::len(v);
while i < n {
f(v[i]);
}
fn sub(t: str, n: int) -> str unsafe {
- let b: str = "";
- let i: uint = 0u;
- let ns: str;
+ let mut b: str = "";
+ let mut i: uint = 0u;
+ let mut ns: str;
alt n {
0 { ns = "no more bottles"; }
1 { ns = "1 bottle"; }
/* Using an interator */
fn ninetynine(it: fn(int)) {
- let n: int = 100;
+ let mut n: int = 100;
while n > 1 { n -= 1; it(n); }
}
fn more(b: bottle) -> bool { alt b { none { ret false; } _ { ret true; } } }
fn main() {
- let b: bottle = multiple(99);
- let running: bool = true;
+ let mut b: bottle = multiple(99);
+ let mut running: bool = true;
while running { show(b); #debug(""); running = more(b); b = next(b); }
}
}
fn sub(t: str, n: int) -> str unsafe {
- let b: str = "";
- let i: uint = 0u;
- let ns: str;
+ let mut b: str = "";
+ let mut i: uint = 0u;
+ let mut ns: str;
alt n {
0 { ns = "no more bottles"; }
1 { ns = "1 bottle"; }
/* Straightforward counter */
fn main() {
- let n: int = 99;
+ let mut n: int = 99;
while n > 0 {
log(debug, sub(b1(), n));
log(debug, sub(b2(), n - 1));
}
fn server(requests: comm::port<request>, responses: comm::chan<uint>) {
- let count = 0u;
- let done = false;
+ let mut count = 0u;
+ let mut done = false;
while !done {
alt comm::recv(requests) {
get_count { comm::send(responses, copy count); }
let workers = option::get(uint::from_str(args[2]));
let start = std::time::precise_time_s();
let to_child = to_child;
- let worker_results = [];
+ let mut worker_results = [];
uint::range(0u, workers) {|_i|
let builder = task::task_builder();
worker_results += [task::future_result(builder)];
8
};
let min_depth = 4;
- let max_depth;
+ let mut max_depth;
if min_depth + 2 > n {
max_depth = min_depth + 2;
} else { max_depth = n; }
stretch_depth,
item_check(stretch_tree)));
let long_lived_tree = bottom_up_tree(0, max_depth);
- let depth = min_depth;
+ let mut depth = min_depth;
while depth <= max_depth {
let iterations = int::pow(2, (max_depth - depth + min_depth) as uint);
- let chk = 0;
- let i = 1;
+ let mut chk = 0;
+ let mut i = 1;
while i <= iterations {
- let temp_tree = bottom_up_tree(i, depth);
+ let mut temp_tree = bottom_up_tree(i, depth);
chk += item_check(temp_tree);
temp_tree = bottom_up_tree(-i, depth);
chk += item_check(temp_tree);
let perm = vec::to_mut(vec::from_elem(n as uint, 0));
let perm1 = vec::to_mut(vec::from_fn(n as uint, perm1init));
let count = vec::to_mut(vec::from_elem(n as uint, 0));
- let f = 0;
- let i = 0;
- let k = 0;
- let r = 0;
- let flips = 0;
- let nperm = 0;
- let checksum = 0;
+ let mut f = 0;
+ let mut i = 0;
+ let mut k = 0;
+ let mut r = 0;
+ let mut flips = 0;
+ let mut nperm = 0;
+ let mut checksum = 0;
r = n;
while r > 0 {
i = 0;
if nperm & 0x1 == 0 { checksum += f; } else { checksum -= f; }
// Use incremental change to generate another permutation
- let go = true;
+ let mut go = true;
while go {
if r == n {
io::println(#fmt("%d", checksum));
type aminoacids = {ch: char, prob: u32};
fn make_cumulative(aa: [aminoacids]) -> [aminoacids] {
- let cp: u32 = 0u32;
- let ans: [aminoacids] = [];
+ let mut cp: u32 = 0u32;
+ let mut ans: [aminoacids] = [];
for a: aminoacids in aa { cp += a.prob; ans += [{ch: a.ch, prob: cp}]; }
ret ans;
}
fn make_random_fasta(id: str, desc: str, genelist: [aminoacids], n: int) {
log(debug, ">" + id + " " + desc);
let rng = @{mutable last: std::rand::rng().next()};
- let op: str = "";
+ let mut op: str = "";
uint::range(0u, n as uint) {|_i|
str::push_char(op, select_random(myrandom_next(rng, 100u32),
genelist));
fn make_repeat_fasta(id: str, desc: str, s: str, n: int) unsafe {
log(debug, ">" + id + " " + desc);
- let op: str = "";
+ let mut op: str = "";
let sl: uint = str::len(s);
uint::range(0u, n as uint) {|i|
str::unsafe::push_byte(op, s[i % sl]);
fn mb(x: cmplx) -> bool
{
- let z = {re: 0., im: 0.};
- let i = 0;
- let in = true;
+ let mut z = {re: 0., im: 0.};
+ let mut i = 0;
+ let mut in = true;
while i < 50 {
z = z*z + x;
if cabs(z) >= 4. {
}
fn fillbyte(x: cmplx, incr: f64) -> u8 {
- let rv = 0_u8;
- let i = 0_u8;
+ let mut rv = 0_u8;
+ let mut i = 0_u8;
while i < 8_u8 {
let z = {re: x.re + (i as f64)*incr, im: x.im};
if mb(z) {
fn chanmb(i: uint, size: uint, ch: comm::chan<line>) -> ()
{
- let crv = [];
+ let mut crv = [];
let incr = 2./(size as f64);
let y = incr*(i as f64) - 1.;
let xincr = 8.*incr;
cout.write_line("P4");
cout.write_line(#fmt("%u %u", size, size));
let lines = std::map::uint_hash();
- let done = 0_u;
- let i = 0_u;
+ let mut done = 0_u;
+ let mut i = 0_u;
while i < size {
let aline = comm::recv(p);
if aline.i == done {
#debug("W %u", aline.i);
cout.write(aline.b);
done += 1_u;
- let prev = done;
+ let mut prev = done;
while prev <= i {
if lines.contains_key(prev) {
#debug("WS %u", prev);
};
let bodies: [Body::props] = NBodySystem::MakeNBodySystem();
io::println(#fmt("%f", NBodySystem::energy(bodies)));
- let i: int = 0;
+ let mut i: int = 0;
while i < n { NBodySystem::advance(bodies, 0.01); i += 1; }
io::println(#fmt("%f", NBodySystem::energy(bodies)));
}
[Body::sun(), Body::jupiter(), Body::saturn(), Body::uranus(),
Body::neptune()];
- let px: float = 0.0;
- let py: float = 0.0;
- let pz: float = 0.0;
+ let mut px: float = 0.0;
+ let mut py: float = 0.0;
+ let mut pz: float = 0.0;
- let i: int = 0;
+ let mut i: int = 0;
while i < 5 {
px += bodies[i].vx * bodies[i].mass;
py += bodies[i].vy * bodies[i].mass;
fn advance(bodies: [Body::props], dt: float) {
- let i: int = 0;
+ let mut i: int = 0;
while i < 5 {
- let j: int = i + 1;
+ let mut j: int = i + 1;
while j < 5 { advance_one(bodies[i], bodies[j], dt); j += 1; }
i += 1;
}
fn energy(bodies: [Body::props]) -> float unsafe {
- let dx: float;
- let dy: float;
- let dz: float;
- let distance: float;
- let e: float = 0.0;
+ let mut dx: float;
+ let mut dy: float;
+ let mut dz: float;
+ let mut distance: float;
+ let mut e: float = 0.0;
- let i: int = 0;
+ let mut i: int = 0;
while i < 5 {
e +=
0.5 * bodies[i].mass *
(bodies[i].vx * bodies[i].vx + bodies[i].vy * bodies[i].vy
+ bodies[i].vz * bodies[i].vz);
- let j: int = i + 1;
+ let mut j: int = i + 1;
while j < 5 {
dx = bodies[i].x - bodies[j].x;
dy = bodies[i].y - bodies[j].y;
}
fn stress_task(&&id: int) {
- let i = 0;
+ let mut i = 0;
loop {
let n = 15;
assert (fib(n) == fib(n));
}
fn stress(num_tasks: int) {
- let results = [];
+ let mut results = [];
range(0, num_tasks) {|i|
let builder = task::task_builder();
results += [task::future_result(builder)];
fn eval_A_times_u(u: [const float], Au: [mutable float]) {
let N = vec::len(u);
- let i = 0u;
+ let mut i = 0u;
while i < N {
Au[i] = 0.0;
- let j = 0u;
+ let mut j = 0u;
while j < N {
Au[i] += eval_A(i, j) * u[j];
j += 1u;
fn eval_At_times_u(u: [const float], Au: [mutable float]) {
let N = vec::len(u);
- let i = 0u;
+ let mut i = 0u;
while i < N {
Au[i] = 0.0;
- let j = 0u;
+ let mut j = 0u;
while j < N {
Au[i] += eval_A(j, i) * u[j];
j += 1u;
let u = vec::to_mut(vec::from_elem(N, 1.0));
let v = vec::to_mut(vec::from_elem(N, 0.0));
- let i = 0u;
+ let mut i = 0u;
while i < 10u {
eval_AtA_times_u(u, v);
eval_AtA_times_u(v, u);
i += 1u;
}
- let vBv = 0.0;
- let vv = 0.0;
- let i = 0u;
+ let mut vBv = 0.0;
+ let mut vv = 0.0;
+ let mut i = 0u;
while i < N {
vBv += u[i] * v[i];
vv += v[i] * v[i];
drop_colors(g, avail, row, col);
// find first remaining color that is available
- let i = 1 as uint;
+ let mut i = 1 as uint;
while i < (10 as uint) { /* FIXME llvm ctlhd */
if bitv::get(avail, i) {
g[row][col] = i as u8;
}
}
- let work: [(u8, u8)] = []; /* queue of uncolored fields */
+ let mut work: [(u8, u8)] = []; /* queue of uncolored fields */
u8::range(0u8, 9u8) { |row|
u8::range(0u8, 9u8) { |col|
let color = (*g)[row][col];
}
}
- let ptr = 0u;
+ let mut ptr = 0u;
let end = vec::len(work);
while (ptr < end) {
let (row, col) = work[ptr];
fn calc(children: uint, parent_ch: comm::chan<msg>) {
let port = comm::port();
let chan = comm::chan(port);
- let child_chs = [];
- let sum = 0;
+ let mut child_chs = [];
+ let mut sum = 0;
iter::repeat (children) {||
task::spawn {||
fn f(&&n: uint) {
- let i = 0u;
+ let mut i = 0u;
while i < n {
task::try {|| g() };
i += 1u;
if vec::len(args) < 2u {
10u
} else { option::get(uint::parse_buf(str::bytes(args[1]), 10u)) };
- let i = 0u;
+ let mut i = 0u;
while i < n { task::spawn {|| f(n); }; i += 1u; }
}
fn f(&&n: uint) {
uint::range(0u, n) {|i|
- let v: [u8] = [];
+ let mut v: [u8] = [];
vec::reserve(v, 1000u);
}
}
}
fn reduce(_word: str, get: map_reduce::getter) {
- let count = 0;
+ let mut count = 0;
loop { alt get() { some(_) { count += 1; } none { break; } } }
}
fn start_mappers(ctrl: chan<ctrl_proto>, -inputs: [str]) ->
[future::future<task::task_result>] {
- let results = [];
+ let mut results = [];
for i: str in inputs {
let builder = task::task_builder();
results += [task::future_result(builder)];
fn emit(im: map::hashmap<str, chan<reduce_proto>>,
ctrl: chan<ctrl_proto>, key: str, val: int) {
- let c;
+ let mut c;
alt im.find(key) {
some(_c) {
c = _c;
// This task becomes the master control task. It task::_spawns
// to do the rest.
- let reducers: map::hashmap<str, chan<reduce_proto>>;
+ let mut reducers: map::hashmap<str, chan<reduce_proto>>;
reducers = map::str_hash();
- let num_mappers = vec::len(inputs) as int;
- let results = start_mappers(chan(ctrl), inputs);
+ let mut num_mappers = vec::len(inputs) as int;
+ let mut results = start_mappers(chan(ctrl), inputs);
while num_mappers > 0 {
alt recv(ctrl) {
num_mappers -= 1;
}
find_reducer(k, cc) {
- let c;
+ let mut c;
// log(error, "finding reducer for " + k);
alt reducers.find(k) {
some(_c) {
map_reduce::map_reduce(inputs);
let stop = time::precise_time_ns();
- let elapsed = stop - start;
+ let mut elapsed = stop - start;
elapsed /= 1000000u64;
log(error, "MapReduce completed in "
}
fn read_word(r: io::reader) -> option<str> {
- let w = "";
+ let mut w = "";
while !r.eof() {
let c = r.read_char();
// error-pattern:wooooo
-fn main() { let a = 1; if 1 == 1 { a = 2; } fail "woooo" + "o"; }
+fn main() {
+ let mut a = 1; if 1 == 1 { a = 2; } fail "woooo" + "o";
+}
}
let map = map::hashmap(hash, eq);
- let arr = [];
+ let mut arr = [];
uint::range(0u, 10u) {|i|
arr += [@"key stuff"];
map.insert(arr, arr + [@"value stuff"]);
import option;
fn foo<T>(y: option<T>) {
- let x: int;
- let rs: [int] = [];
+ let mut x: int;
+ let mut rs: [int] = [];
/* tests that x doesn't get put in the precondition for the
entire if expression */
fn foo(it: fn(int)) { it(10); }
fn main() {
- let x = true;
+ let mut x = true;
alt a {
a { x = true; foo {|_i|} }
b { x = false; }
}
fn process(c: color) -> int {
- let x: int;
+ let mut x: int;
alt c {
rgb(r, _, _) { #debug("rgb"); log(debug, r); x = r; }
rgba(_, _, _, a) { #debug("rgba"); log(debug, a); x = a; }
fn f2(a: int, f: fn(int)) -> int { f(1); ret a; }
fn main() {
- let a = {mutable x: 1}, b = 2, c = 3;
+ let mut a = {mutable x: 1}, b = 2, c = 3;
assert (f1(a, b, c) == 6);
assert (a.x == 0);
assert (b == 10);
// Issue 483 - Assignment expressions result in nil
fn test_assign() {
- let x: int;
- let y: () = x = 10;
+ let mut x: int;
+ let mut y: () = x = 10;
assert (x == 10);
- let z = x = 11;
+ let mut z = x = 11;
assert (x == 11);
z = x = 12;
assert (x == 12);
}
fn test_assign_op() {
- let x: int = 0;
- let y: () = x += 10;
+ let mut x: int = 0;
+ let mut y: () = x += 10;
assert (x == 10);
- let z = x += 11;
+ let mut z = x += 11;
assert (x == 21);
z = x += 12;
assert (x == 33);
fn main() {
- let sum = 0;
+ let mut sum = 0;
for x in [1, 2, 3, 4, 5] { sum += x; }
assert (sum == 15);
}
let ch = chan(p);
task::spawn {|| a(ch); };
task::spawn {|| a(ch); };
- let n: int = 0;
+ let mut n: int = 0;
n = recv(p);
n = recv(p);
// #debug("Finished.");
let ch = chan(p);
task::spawn {|| a(ch); };
task::spawn {|| b(ch); };
- let n: int = 0;
+ let mut n: int = 0;
n = recv(p);
n = recv(p);
#debug("Finished.");
}
fn main() {
- let n: int = 2 + 3 * 7;
+ let mut n: int = 2 + 3 * 7;
let s: str = "hello there";
let p = comm::port();
let ch = comm::chan(p);
task::spawn {|| a(ch); };
task::spawn {|| b(ch); };
- let x: int = 10;
+ let mut x: int = 10;
x = g(n, s);
log(debug, x);
n = recv(p);
}
fn general() {
- let a: int = 1;
- let b: int = 2;
+ let mut a: int = 1;
+ let mut b: int = 2;
a ^= b;
b ^= a;
a = a ^ b;
}
// Usable at all:
- let any_negative = vec::any(v) { |e| float::is_negative(e) };
+ let mut any_negative = vec::any(v) { |e| float::is_negative(e) };
assert any_negative;
// Higher precedence than assignments:
fn main() {
let v = [1, 2, 3, 4, 5, 6, 7];
- let odds = 0;
+ let mut odds = 0;
iter_vec(v, {|i|
log(error, i);
if i % 2 == 1 {
fn main() {
let v = [1, 2, 3, 4, 5];
- let sum = 0;
+ let mut sum = 0;
iter_vec(v, {|i|
iter_vec(v, {|j|
log(error, i * j);
fn main() {
- let i = 0;
+ let mut i = 0;
while i < 20 { i += 1; if i == 10 { break; } }
assert (i == 10);
do { i += 1; if i == 20 { break; } } while i < 30
fn request_task(c: chan<ctx>) {
let p = port();
send(c, chan(p));
- let req: request;
+ let mut req: request;
req = recv(p);
// Need to drop req before receiving it again
req = recv(p);
let p = port();
let ch = chan(p);
let t = task::spawn {|| request_task(ch); };
- let cx: ctx;
+ let mut cx: ctx;
cx = recv(p);
ret cx;
}
fn putstr(s: str) { }
fn putint(i: int) {
- let i: int = 33;
+ let mut i: int = 33;
while i < 36 { putstr("hi"); i = i + 1; }
}
fn zerg(i: int) -> int { ret i; }
fn foo(x: int) -> int {
- let y: t = x + 2;
+ let mut y: t = x + 2;
putstr("hello");
while y < 10 { putint(y); if y * 3 == 4 { y = y + 2; nothing(); } }
- let z: t;
+ let mut z: t;
z = 0x55;
foo(z);
ret 0;
fn foo(_a: uint, _b: uint) : uint::le(_a, _b) {}
let a: uint = 1u;
let b: uint = 4u;
- let c: uint = 17u;
+ let mut c: uint = 17u;
check (uint::le(a, b));
c <- a;
log(debug, foo(c, b));
fn main() unsafe {
fn foo(_a: uint, _b: uint) : uint::le(_a, _b) {}
- let a: uint = 4u;
- let b: uint = 1u;
+ let mut a: uint = 4u;
+ let mut b: uint = 1u;
check (uint::le(b, a));
b <-> a;
log(debug, foo(a, b));
// Tests for using alt as an expression
fn test_basic() {
- let rs: bool = alt true { true { true } false { false } };
+ let mut rs: bool = alt true { true { true } false { false } };
assert (rs);
rs = alt false { true { false } false { true } };
assert (rs);
}
fn test_inferrence() {
- let rs = alt true { true { true } false { false } };
+ let mut rs = alt true { true { true } false { false } };
assert (rs);
}
fn test_rec() { let rs = { {v1: 10, v2: 20} }; assert (rs.v2 == 20); }
fn test_filled_with_stuff() {
- let rs = { let a = 0; while a < 10 { a += 1; } a };
+ let rs = { let mut a = 0; while a < 10 { a += 1; } a };
assert (rs == 10);
}
}
fn main() {
- let called = false;
+ let mut called = false;
let h = f;
g(h, called);
assert called == true;
fn main() {
let a: [mutable int] = [mutable -1, -1, -1, -1];
- let p: int = 0;
+ let mut p: int = 0;
two {|i|
two {|j| a[p] = 10 * i + j; p += 1; };
};
fn pairs(it: fn((int, int))) {
- let i: int = 0;
- let j: int = 0;
+ let mut i: int = 0;
+ let mut j: int = 0;
while i < 10 { it((i, j)); i += 1; j += i; }
}
fn main() {
- let i: int = 10;
- let j: int = 0;
+ let mut i: int = 10;
+ let mut j: int = 0;
pairs() {|p|
let (_0, _1) = p;
log(debug, _0);
// -*- rust -*-
fn main() {
- let sum: int = 0;
+ let mut sum: int = 0;
first_ten {|i| #debug("main"); log(debug, i); sum = sum + i; };
#debug("sum");
log(debug, sum);
}
fn first_ten(it: fn(int)) {
- let i: int = 0;
+ let mut i: int = 0;
while i < 10 { #debug("first_ten"); it(i); i = i + 1; }
}
type triple = {x: int, y: int, z: int};
fn main() {
- let x = 62;
- let y = 63;
+ let mut x = 62;
+ let mut y = 63;
let a = 'a';
- let b = 'b';
+ let mut b = 'b';
let p: triple = {x: 65, y: 66, z: 67};
- let q: triple = {x: 68, y: 69, z: 70};
+ let mut q: triple = {x: 68, y: 69, z: 70};
y = id::<int>(x);
log(debug, y);
assert (x == y);
enum foo<T> { arm(T), }
fn altfoo<T>(f: foo<T>) {
- let hit = false;
+ let mut hit = false;
alt f { arm::<T>(x) { #debug("in arm"); hit = true; } }
assert (hit);
}
enum option<T> { some(@T), none, }
-fn main() { let a: option<int> = some::<int>(@10); a = none::<int>; }
+fn main() { let mut a: option<int> = some::<int>(@10); a = none::<int>; }
fn emit(im: map::hashmap<str, int>, ctrl: chan<ctrl_proto>, key: str,
val: str) {
- let c;
+ let mut c;
alt im.find(key) {
some(_c) { c = _c }
none {
// This task becomes the master control task. It spawns others
// to do the rest.
- let reducers: map::hashmap<str, int>;
+ let mut reducers: map::hashmap<str, int>;
reducers = map::str_hash();
start_mappers(chan(ctrl), inputs);
- let num_mappers = vec::len(inputs) as int;
+ let mut num_mappers = vec::len(inputs) as int;
while num_mappers > 0 {
alt recv(ctrl) {
mapper_done { num_mappers -= 1; }
find_reducer(k, cc) {
- let c;
+ let mut c;
alt reducers.find(str::from_bytes(k)) {
some(_c) { c = _c; }
none { c = 0; }
// -*- rust -*-
-fn main() { let x: i32 = -400_i32; x = 0_i32 - x; assert (x == 400_i32); }
+fn main() { let mut x: i32 = -400_i32; x = 0_i32 - x; assert (x == 400_i32); }
// -*- rust -*-
fn main() {
- let x: i8 = -12i8;
+ let mut x: i8 = -12i8;
let y: i8 = -12i8;
x = x + 1i8;
x = x - 1i8;
}
impl <T> of map<T> for [T] {
fn map<U>(f: fn(T) -> U) -> [U] {
- let r = [];
+ let mut r = [];
for x in self { r += [f(x)]; }
r
}
import vec::*;
fn main() {
- let v = from_elem(0u, 0);
+ let mut v = from_elem(0u, 0);
v += [4, 2];
assert (reversed(v) == [2, 4]);
}
}
fn main() unsafe {
- let v: [int] = [];
+ let mut v: [int] = [];
assert (vec_len(v) == 0u); // zero-length
- let x = [1, 2];
+ let mut x = [1, 2];
assert (vec_len(x) == 2u); // on stack
- let y = [1, 2, 3, 4, 5];
+ let mut y = [1, 2, 3, 4, 5];
assert (vec_len(y) == 5u); // on heap
v += [];
fn main () {
- let line = "";
- let i = 0;
+ let mut line = "";
+ let mut i = 0;
do {
line = if i == 9 { "exit" } else { "notexit" };
i += 1;
spawn {|| b(ch); };
recv(p);
}
- let i = 0;
+ let mut i = 0;
while i < 100 {
doit();
i += 1;
}
fn test_append() {
- let s = "";
+ let mut s = "";
s += "a";
assert (s == "a");
- let s = "a";
+ let mut s = "a";
s += "b";
log(debug, s);
assert (s == "ab");
- let s = "c";
+ let mut s = "c";
s += "offee";
assert (s == "coffee");
fn range(a: int, b: int, it: fn(int)) {
assert (a < b);
- let i: int = a;
+ let mut i: int = a;
while i < b { it(i); i += 1; }
}
fn main() {
- let sum: int = 0;
+ let mut sum: int = 0;
range(0, 100) {|x| sum += x; }
log(debug, sum);
}
fn double_int(a: int) -> [int] { ret [a] + [a]; }
fn main() {
- let d = double(1);
+ let mut d = double(1);
assert (d[0] == 1);
assert (d[1] == 1);
call_me({|| *q}, q);
// Check that no false positives are found in loops.
- let q = ~40, p = 10;
+ let mut q = ~40, p = 10;
loop {
let i = q;
p += *i;
fn main() {
let x = 1 == 2 || 3 == 3;
assert (x);
- let y: int = 10;
+ let mut y: int = 10;
log(debug, x || incr(y));
assert (y == 10);
if true && x { assert (true); } else { assert (false); }
fn foo(x: int) { log(debug, x); }
-fn main() { let x: int; if 1 > 2 { x = 12; } else { x = 10; } foo(x); }
+fn main() { let mut x: int; if 1 > 2 { x = 12; } else { x = 10; } foo(x); }
fn main() {
let p = port();
let ch = chan(p);
- let y: int;
+ let mut y: int;
task::spawn {|| child(ch); };
y = recv(p);
enum t { a, b(@int), }
-fn main() { let x = b(@10); x = a; }
+fn main() { let mut x = b(@10); x = a; }
fn main() {
let u = {x: 10, y: @{a: 20}};
- let {x: x, y: @{a: a}} = u;
+ let mut {x: x, y: @{a: a}} = u;
x = 100;
a = 100;
assert (x == 100);
fn main() {
let x = [1, 2, 3];
- let y = 0;
+ let mut y = 0;
for i: int in x { log(debug, i); y += i; }
log(debug, y);
assert (y == 6);
let s = "hello there";
- let i: int = 0;
+ let mut i: int = 0;
for c: u8 in s {
if i == 0 { assert (c == 'h' as u8); }
if i == 1 { assert (c == 'e' as u8); }
-fn main() { let i: int = 0; while i < 1000000 { i += 1; let x = 3; } }
+fn main() { let mut i: int = 0; while i < 1000000 { i += 1; let x = 3; } }
fn main() {
- let i = 0u;
+ let mut i = 0u;
loop {
log(error, "a");
i += 1u;
}
}
assert (i == 10u);
- let is_even = false;
+ let mut is_even = false;
loop {
if i == 21u {
break;
fn main() {
let x = [10, 20, 30];
- let sum = 0;
+ let mut sum = 0;
for x in x { sum += x; }
assert (sum == 60);
}
// -*- rust -*-
fn len(v: [const int]) -> uint {
- let i = 0u;
+ let mut i = 0u;
for x: int in v { i += 1u; }
ret i;
}
impl <A> of monad<A> for [A] {
fn bind<B>(f: fn(A) -> [B]) -> [B] {
- let r = [];
+ let mut r = [];
for elt in self { r += f(elt); }
r
}
}
fn main() {
- let sz = 400u;
+ let mut sz = 400u;
while sz < 500u {
task::try {|| getbig(200) };
sz += 1u;
fn test(x: bool, foo: ~{x: int, y: int, z: int}) -> int {
let bar = foo;
- let y: ~{x: int, y: int, z: int};
+ let mut y: ~{x: int, y: int, z: int};
if x { y <- bar; } else { y = ~{x: 4, y: 5, z: 6}; }
ret y.y;
}
fn test(x: bool, foo: @{x: int, y: int, z: int}) -> int {
let bar = foo;
- let y: @{x: int, y: int, z: int};
+ let mut y: @{x: int, y: int, z: int};
if x { y <- bar; } else { y = @{x: 4, y: 5, z: 6}; }
ret y.y;
}
fn test(x: bool, foo: ~{x: int, y: int, z: int}) -> int {
let bar = foo;
- let y: ~{x: int, y: int, z: int};
+ let mut y: ~{x: int, y: int, z: int};
if x { y <- bar; } else { y = ~{x: 4, y: 5, z: 6}; }
ret y.y;
}
fn test(x: bool, foo: @{x: int, y: int, z: int}) -> int {
let bar = foo;
- let y: @{x: int, y: int, z: int};
+ let mut y: @{x: int, y: int, z: int};
if x { y <- bar; } else { y = @{x: 4, y: 5, z: 6}; }
ret y.y;
}
fn main() {
let y: int = 42;
- let x: int;
+ let mut x: int;
x <- y;
assert (x == 42);
}
fn grow(&v: [int]) { v += [1]; }
fn main() {
- let v: [int] = [];
+ let mut v: [int] = [];
grow(v);
grow(v);
grow(v);
fn foo() {
alt some::<int>(5) {
some::<int>(x) {
- let bar;
+ let mut bar;
alt none::<int> { none::<int> { bar = 5; } _ { baz(); } }
log(debug, bar);
}
// -*- rust -*-
fn main() {
- let x: int = 1;
+ let mut x: int = 1;
x *= 2;
log(debug, x);
assert (x == 2);
}
fn main() {
- let p = {x: 10, y: 20};
+ let mut p = {x: 10, y: 20};
p += {x: 101, y: 102};
p -= {x: 100, y: 100};
assert p + {x: 5, y: 5} == {x: 16, y: 27};
fn ret_ext_ext_mem() -> @{a: @int, b: @int} { ret @{a: @10, b: @10}; }
fn main() {
- let int_i: int;
- let ext_i: @int;
- let int_rec: {a: int, b: int};
- let ext_rec: @{a: int, b: int};
- let ext_mem: {a: @int, b: @int};
- let ext_ext_mem: @{a: @int, b: @int};
+ let mut int_i: int;
+ let mut ext_i: @int;
+ let mut int_rec: {a: int, b: int};
+ let mut ext_rec: @{a: int, b: int};
+ let mut ext_mem: {a: @int, b: @int};
+ let mut ext_ext_mem: @{a: @int, b: @int};
int_i = ret_int_i(); // initializing
int_i = ret_int_i(); // non-initializing
// Regression test for issue #152.
fn main() {
- let b: uint = 1u;
+ let mut b: uint = 1u;
while b <= 32u {
0u << b;
b <<= 1u;
fn main() {
let x = true;
- if x { let i = 10; while i > 0 { i -= 1; } }
+ if x { let mut i = 10; while i > 0 { i -= 1; } }
alt x { true { #debug("right"); } false { #debug("wrong"); } }
}
fn main() {
let x = 3;
let y = int_wrapper_ctor(&x);
- let z : ∫
+ let mut z : ∫
alt y {
int_wrapper_ctor(zz) { z = zz; }
}
// -*- rust -*-
fn foo(c: [int]) {
let a: int = 5;
- let b: [int] = [];
+ let mut b: [int] = [];
alt none::<int> {
-fn main() { let n; n = 1; log(debug, n); }
+fn main() { let mut n; n = 1; log(debug, n); }
task::spawn {|| x("hello from first spawned fn", 65); };
task::spawn {|| x("hello from second spawned fn", 66); };
task::spawn {|| x("hello from third spawned fn", 67); };
- let i: int = 30;
+ let mut i: int = 30;
while i > 0 { i = i - 1; #debug("parent sleeping"); yield(); }
}
impl util for uint {
fn str() -> str { uint::str(self) }
fn times(f: fn(uint)) {
- let c = 0u;
+ let mut c = 0u;
while c < self { f(c); c += 1u; }
}
}
fn length() -> uint { vec::len(self) }
fn iter(f: fn(T)) { for x in self { f(x); } }
fn map<U>(f: fn(T) -> U) -> [U] {
- let r = [];
+ let mut r = [];
for elt in self { r += [f(elt)]; }
r
}
assert [1].length().str() == "1";
assert [3, 4].map({|a| a + 4})[0] == 7;
assert [3, 4].map::<uint>({|a| a as uint + 4u})[0] == 7u;
- let x = 0u;
+ let mut x = 0u;
10u.times {|_n| x += 2u;}
assert x == 20u;
}
import str;
fn test1() {
- let s: str = "hello";
+ let mut s: str = "hello";
s += "world";
log(debug, s);
assert (s[9] == 'd' as u8);
fn main() {
- let s = "a";
+ let mut s = "a";
s += "b";
assert (s[0] == 'a' as u8);
assert (s[1] == 'b' as u8);
fn main() {
// Make sure we properly handle repeated self-appends.
- let a: str = "A";
- let i = 20;
- let expected_len = 1u;
+ let mut a: str = "A";
+ let mut i = 20;
+ let mut expected_len = 1u;
while i > 0 {
log(error, str::len(a));
assert (str::len(a) == expected_len);
-fn main() { let x = 3; let y = 7; x <-> y; assert (x == 7); assert (y == 3); }
+fn main() {
+ let mut x = 3; let mut y = 7;
+ x <-> y; assert (x == 7); assert (y == 3);
+}
swap(a, 2, 4);
assert (a[2] == 4);
assert (a[4] == 2);
- let n = 42;
+ let mut n = 42;
n <-> a[0];
assert (a[0] == 42);
assert (n == 0);
let po = comm::port();
let ch = comm::chan(po);
task::spawn {|| test05_start(ch); };
- let value = comm::recv(po);
+ let mut value = comm::recv(po);
log(error, value);
value = comm::recv(po);
log(error, value);
let p = comm::port();
comm::send(c, comm::chan(p));
- let a;
- let b;
+ let mut a;
+ let mut b;
a = comm::recv(p);
assert a == "A";
log(error, a);
task::run(builder) {|| start(i); };
// Sleep long enough for the task to finish.
- let i = 0;
+ let mut i = 0;
while i < 10000 {
task::yield();
i += 1;
import comm::send;
fn start(c: comm::chan<int>, start: int, number_of_messages: int) {
- let i: int = 0;
+ let mut i: int = 0;
while i < number_of_messages { send(c, start + i); i += 1; }
}
let ch = comm::chan(po);
// Spawn 10 tasks each sending us back one int.
- let i = 10;
+ let mut i = 10;
while (i > 0) {
log(debug, i);
task::spawn {|| child(i, ch); };
// anything back, so we deadlock here.
i = 10;
- let value = 0;
+ let mut value = 0;
while (i > 0) {
log(debug, i);
value = comm::recv(po);
import task;
fn start(c: comm::chan<int>, i0: int) {
- let i = i0;
+ let mut i = i0;
while i > 0 {
comm::send(c, 0);
i = i - 1;
let ch = chan(po);
let r0: r = {val0: 0, val1: 1u8, val2: '2'};
send(ch, r0);
- let r1: r;
+ let mut r1: r;
r1 = recv(po);
assert (r1.val0 == 0);
assert (r1.val1 == 1u8);
send(ch, tag2(10));
send(ch, tag3(10, 11u8, 'A'));
// FIXME: Do port semantics really guarantee these happen in order?
- let t1: t;
+ let mut t1: t;
t1 = recv(po);
assert (t1 == tag1);
t1 = recv(po);
// Does the transmitted channel still work?
send(ch1, 10);
- let i: int;
+ let mut i: int;
i = recv(po0);
assert (i == 10);
}
fn test00_start(ch: chan<int>, message: int, count: int) {
#debug("Starting test00_start");
- let i: int = 0;
+ let mut i: int = 0;
while i < count {
#debug("Sending Message");
send(ch, message + 0);
let po = comm::port();
let ch = chan(po);
- let i: int = 0;
+ let mut i: int = 0;
// Create and spawn tasks...
- let results = [];
+ let mut results = [];
while i < number_of_tasks {
let builder = task::task_builder();
results += [task::future_result(builder)];
}
// Read from spawned tasks...
- let sum = 0;
+ let mut sum = 0;
for r in results {
i = 0;
while i < number_of_messages {
fn main() { test00(); }
fn test00() {
- let r: int = 0;
- let sum: int = 0;
+ let mut r: int = 0;
+ let mut sum: int = 0;
let p = comm::port();
let c = comm::chan(p);
send(c, 1);
fn test00() {
let r: int = 0;
- let sum: int = 0;
+ let mut sum: int = 0;
let p = comm::port();
let c = comm::chan(p);
let number_of_messages: int = 1000;
- let i: int = 0;
+ let mut i: int = 0;
while i < number_of_messages { comm::send(c, i + 0); i += 1; }
i = 0;
while i < number_of_messages { sum += comm::recv(p); i += 1; }
fn main() { test00(); }
fn test00() {
- let r: int = 0;
- let sum: int = 0;
+ let mut r: int = 0;
+ let mut sum: int = 0;
let p = comm::port();
let c0 = chan(p);
let c1 = chan(p);
let c2 = chan(p);
let c3 = chan(p);
let number_of_messages: int = 1000;
- let i: int = 0;
+ let mut i: int = 0;
while i < number_of_messages {
send(c0, i + 0);
send(c1, i + 0);
fn main() { test00(); }
fn test00_start(c: comm::chan<int>, start: int, number_of_messages: int) {
- let i: int = 0;
+ let mut i: int = 0;
while i < number_of_messages { comm::send(c, start + i); i += 1; }
}
fn test00() {
- let r: int = 0;
- let sum: int = 0;
+ let mut r: int = 0;
+ let mut sum: int = 0;
let p = comm::port();
let number_of_messages: int = 10;
let c = comm::chan(p);
test00_start(c, number_of_messages * 3, number_of_messages);
}
- let i: int = 0;
+ let mut i: int = 0;
while i < number_of_messages {
r = comm::recv(p);
sum += r;
fn main() { test00(); }
fn test00_start(c: comm::chan<int>, number_of_messages: int) {
- let i: int = 0;
+ let mut i: int = 0;
while i < number_of_messages { comm::send(c, i + 0); i += 1; }
}
fn test00() {
let r: int = 0;
- let sum: int = 0;
+ let mut sum: int = 0;
let p = comm::port();
let number_of_messages: int = 10;
let ch = comm::chan(p);
test00_start(ch, number_of_messages);
}
- let i: int = 0;
+ let mut i: int = 0;
while i < number_of_messages {
sum += comm::recv(p);
log(debug, r);
fn test00_start(ch: chan<int>, message: int, count: int) {
#debug("Starting test00_start");
- let i: int = 0;
+ let mut i: int = 0;
while i < count {
#debug("Sending Message");
send(ch, message + 0);
let po = port();
let ch = chan(po);
- let i: int = 0;
+ let mut i: int = 0;
- let results = [];
+ let mut results = [];
while i < number_of_tasks {
i = i + 1;
let builder = task::task_builder();
results += [task::future_result(builder)];
task::run(builder) {|| test00_start(ch, i, number_of_messages);}
}
- let sum: int = 0;
+ let mut sum: int = 0;
for r in results {
i = 0;
while i < number_of_messages { sum += recv(po); i = i + 1; }
fn test04_start() {
#debug("Started task");
- let i: int = 1024 * 1024;
+ let mut i: int = 1024 * 1024;
while i > 0 { i = i - 1; }
#debug("Finished task");
}
fn test04() {
#debug("Spawning lots of tasks.");
- let i: int = 4;
+ let mut i: int = 4;
while i > 0 { i = i - 1; task::spawn {|| test04_start(); }; }
#debug("Finishing up.");
}
let po = comm::port();
let ch = chan(po);
task::spawn {|| test05_start(ch); };
- let value: int;
+ let mut value: int;
value = recv(po);
value = recv(po);
value = recv(po);
fn test06_start(&&task_number: int) {
#debug("Started task.");
- let i: int = 0;
+ let mut i: int = 0;
while i < 1000000 { i = i + 1; }
#debug("Finished task.");
}
let number_of_tasks: int = 4;
#debug("Creating tasks");
- let i: int = 0;
+ let mut i: int = 0;
- let results = [];
+ let mut results = [];
while i < number_of_tasks {
i = i + 1;
let builder = task::task_builder();
fn test_break() { loop { let x: @int = break; } }
-fn test_cont() { let i = 0; while i < 1 { i += 1; let x: @int = cont; } }
+fn test_cont() { let mut i = 0; while i < 1 { i += 1; let x: @int = cont; } }
fn test_ret() { let x: @int = ret; }
import task;
fn main() {
- let i = 10;
+ let mut i = 10;
while i > 0 { task::spawn {|| child(i); }; i = i - 1; }
#debug("main thread exiting");
}
fn range(lo: uint, hi: uint, it: fn(uint)) {
- let lo_ = lo;
+ let mut lo_ = lo;
while lo_ < hi { it(lo_); lo_ += 1u; }
}
-fn f() { let x = 10; let y = 11; if true { alt x { _ { y = x; } } } else { } }
+fn f() {
+ let x = 10; let mut y = 11;
+ if true { alt x { _ { y = x; } } } else { }
+}
fn main() {
let x = 10;
- let y = 11;
+ let mut y = 11;
if true { while false { y = x; } } else { }
}
// -*- rust -*-
fn main() {
- let word: u32 = 200000u32;
+ let mut word: u32 = 200000u32;
word = word - 1u32;
assert (word == 199999u32);
}
// These constants were chosen because they aren't used anywhere
// in the rest of the generated code so they're easily grep-able.
fn main() {
- let x: u8 = 19u8; // 0x13
+ let mut x: u8 = 19u8; // 0x13
- let y: u8 = 35u8; // 0x23
+ let mut y: u8 = 35u8; // 0x23
x = x + 7u8; // 0x7
// -*- rust -*-
fn main() {
- let x: u8 = 12u8;
+ let mut x: u8 = 12u8;
let y: u8 = 12u8;
x = x + 1u8;
x = x - 1u8;
fn main() {
let i = ~mutable 1;
// Should be a copy
- let j;
+ let mut j;
j = i;
*i = 2;
*j = 3;
fn main() {
let i = ~1;
- let j = ~2;
+ let mut j = ~2;
// Should drop the previous value of j
j = i;
assert *j == 1;
fn main() {
- let i;
+ let mut i;
i = ~1;
assert *i == 1;
}
\ No newline at end of file
}
fn main() {
- let i = ~100;
+ let mut i = ~100;
f(i);
assert *i == 200;
}
\ No newline at end of file
fn main() {
- let i;
+ let mut i;
i <- ~100;
assert *i == 100;
}
\ No newline at end of file
fn main() {
let i = ~100;
- let j;
+ let mut j;
j <- i;
assert *j == 100;
}
\ No newline at end of file
let p = comm::port();
let ch = comm::chan(p);
let n = 100u;
- let expected = 0u;
+ let mut expected = 0u;
uint::range(0u, n) {|i|
task::spawn {|| child(ch, i); };
expected += i;
}
- let actual = 0u;
+ let mut actual = 0u;
uint::range(0u, n) {|_i|
let j = comm::recv(p);
actual += *j;
fn main() {
- let i = ~100;
- let j = ~200;
+ let mut i = ~100;
+ let mut j = ~200;
i <-> j;
assert i == ~200;
assert j == ~100;
// -*- rust -*-
fn f(u: ()) { ret u; }
-fn main() { let u1: () = (); let u2: () = f(u1); u2 = (); ret (); }
+fn main() {
+ let u1: () = ();
+ let mut u2: () = f(u1);
+ u2 = ();
+ ret ();
+}
fn vec_ret() { let _v = [1, 2, ret, 4]; }
fn fail_then_concat() {
- let x = [], y = [3];
+ let mut x = [], y = [3];
fail;
x += y;
"good" + "bye";
fn foo<T>(o: myoption<T>) -> int {
- let x: int = 5;
+ let mut x: int = 5;
alt o { none::<T> { } some::<T>(t) { x += 1; } }
ret x;
}
fn foo<T>(o: myoption<T>) -> int {
- let x: int;
+ let mut x: int;
alt o { none::<T> { fail; } some::<T>(t) { x = 5; } }
ret x;
}
assert (oo as int == 0xd6);
fn check_str_eq(a: str, b: str) {
- let i: int = 0;
+ let mut i: int = 0;
for ab: u8 in a {
log(debug, i);
log(debug, ab);
assert (!str::is_utf8([0xc0_u8]));
assert (!str::is_utf8([0xc0_u8, 0x10_u8]));
- let stack = "a×c€";
+ let mut stack = "a×c€";
assert (str::pop_char(stack) == '€');
assert (str::pop_char(stack) == 'c');
str::push_char(stack, 'u');
fn main() {
- let v = [1];
+ let mut v = [1];
v += [2];
v += [3];
v += [4];
-fn main() { let a = [1, 2, 3, 4, 5]; let b = [a, a]; b += b; }
+fn main() { let a = [1, 2, 3, 4, 5]; let mut b = [a, a]; b += b; }
fn main() {
- let later: [int];
+ let mut later: [int];
if true { later = [1]; } else { later = [2]; }
log(debug, later[0]);
}
fn push<T: copy>(&v: [const T], t: T) { v += [t]; }
-fn main() { let v = [1, 2, 3]; push(v, 1); }
+fn main() { let mut v = [1, 2, 3]; push(v, 1); }
fn test_heap_to_heap() {
// a spills onto the heap
- let a = [0, 1, 2, 3, 4];
+ let mut a = [0, 1, 2, 3, 4];
a += a;
assert (vec::len(a) == 10u);
assert (a[0] == 0);
fn test_stack_to_heap() {
// a is entirely on the stack
- let a = [0, 1, 2];
+ let mut a = [0, 1, 2];
// a spills to the heap
a += a;
assert (vec::len(a) == 6u);
fn test_loop() {
// Make sure we properly handle repeated self-appends.
- let a: [int] = [0];
- let i = 20;
- let expected_len = 1u;
+ let mut a: [int] = [0];
+ let mut i = 20;
+ let mut expected_len = 1u;
while i > 0 {
log(error, vec::len(a));
assert (vec::len(a) == expected_len);
let v: [int] = [10, 20];
assert (v[0] == 10);
assert (v[1] == 20);
- let x: int = 0;
+ let mut x: int = 0;
assert (v[x] == 10);
assert (v[x + 1] == 20);
x = x + 1;
}
fn notsure() {
- let _x;
- let _y = (_x = 0) == (_x = 0);
- let _z = (_x <- 0) < (_x = 0);
+ let mut _x;
+ let mut _y = (_x = 0) == (_x = 0);
+ let mut _z = (_x <- 0) < (_x = 0);
let _a = (_x += 0) == (_x = 0);
let _b = (_y <-> _z) == (_y <-> _z);
}
fn angrydome() {
loop { if break { } }
- let i = 0;
+ let mut i = 0;
do { i += 1; if i == 1 { alt cont { _ { } } } } while false
}
fn main() {
- let x: int = 10;
- let y: int = 0;
+ let mut x: int = 10;
+ let mut y: int = 0;
while y < x { log(debug, y); #debug("hello"); y = y + 1; }
do {
#debug("goodbye");
// Issue #825: Should recheck the loop contition after continuing
fn main() {
- let i = 1;
+ let mut i = 1;
while i > 0 {
assert (i > 0);
log(debug, i);
fn main() {
- let y: int = 42;
- let z: int = 42;
- let x: int;
+ let mut y: int = 42;
+ let mut z: int = 42;
+ let mut x: int;
while z < 50 {
z += 1;
while false { x <- y; y = z; }
fn make(i: int) -> t {
if i > 10 { ret a; }
- let s = "hello";
+ let mut s = "hello";
// Ensure s is non-const.
s += "there";
}
fn main() {
- let i = 0;
+ let mut i = 0;
// The auto slot for the result of make(i) should not leak.
// -*- rust -*-
fn main() {
- let i: int = 90;
+ let mut i: int = 90;
while i < 100 {
log(debug, i);
i = i + 1;
fn f(&p: point) { p.z = 13; }
fn main() {
- let x: point = {x: 10, y: 11, mutable z: 12};
+ let mut x: point = {x: 10, y: 11, mutable z: 12};
f(x);
assert (x.z == 13);
}
use std;
fn main() {
- let i: int = 0;
+ let mut i: int = 0;
while i < 100 { i = i + 1; log(error, i); task::yield(); }
}