Skip to content

migrate many for loops to foreach #8184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/po/rust.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1792,11 +1792,11 @@ msgstr ""
msgid ""
"~~~~ {.xfail-test}\n"
"fn iter<T>(seq: &[T], f: &fn(T)) {\n"
" for seq.iter().advance |elt| { f(elt); }\n"
" foreach elt in seq.iter() { f(elt); }\n"
"}\n"
"fn map<T, U>(seq: &[T], f: &fn(T) -> U) -> ~[U] {\n"
" let mut acc = ~[];\n"
" for seq.iter().advance |elt| { acc.push(f(elt)); }\n"
" foreach elt in seq.iter() { acc.push(f(elt)); }\n"
" acc\n"
"}\n"
"~~~~\n"
Expand Down Expand Up @@ -4570,7 +4570,7 @@ msgstr ""
#: doc/rust.md:2405
#, no-wrap
msgid ""
"for v.iter().advance |e| {\n"
"foreach e in v.iter() {\n"
" bar(*e);\n"
"}\n"
"~~~~\n"
Expand Down
10 changes: 5 additions & 5 deletions doc/po/tutorial-container.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ msgstr ""
#, no-wrap
msgid ""
"// print out all the elements in the vector\n"
"for xs.iter().advance |x| {\n"
"foreach x in xs.iter() {\n"
" println(x.to_str())\n"
"}\n"
msgstr ""
Expand All @@ -386,7 +386,7 @@ msgstr ""
#, no-wrap
msgid ""
"// print out all but the first 3 elements in the vector\n"
"for xs.iter().skip(3).advance |x| {\n"
"foreach x in xs.iter().skip(3) {\n"
" println(x.to_str())\n"
"}\n"
"~~~\n"
Expand Down Expand Up @@ -418,7 +418,7 @@ msgstr ""
#, no-wrap
msgid ""
"// print out the pairs of elements up to (&3, &\"baz\")\n"
"for it.advance |(x, y)| {\n"
"foreach (x, y) in it {\n"
" println(fmt!(\"%d %s\", *x, *y));\n"
msgstr ""

Expand Down Expand Up @@ -487,7 +487,7 @@ msgid ""
" pub fn from_iterator(iterator: &mut T) -> ~[A] {\n"
" let (lower, _) = iterator.size_hint();\n"
" let mut xs = with_capacity(lower);\n"
" for iterator.advance |x| {\n"
" foreach x in iterator {\n"
" xs.push(x);\n"
" }\n"
" xs\n"
Expand Down Expand Up @@ -587,7 +587,7 @@ msgstr ""
#, no-wrap
msgid ""
"// prints `5`, `4` and `3`\n"
"for it.invert().advance |&x| {\n"
"foreach &x in it.invert() {\n"
" println(fmt!(\"%?\", x))\n"
"}\n"
"~~~\n"
Expand Down
2 changes: 1 addition & 1 deletion doc/po/tutorial-tasks.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ msgstr ""
#, no-wrap
msgid ""
" let mut final_res = 0f64;\n"
" for futures.mut_iter().advance |ft| {\n"
" foreach ft in futures.mut_iter() {\n"
" final_res += ft.get();\n"
" }\n"
" println(fmt!(\"^2/6 is not far from : %?\", final_res));\n"
Expand Down
10 changes: 5 additions & 5 deletions doc/po/tutorial.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,7 @@ msgstr ""
msgid ""
"// Iterate over a vector, obtaining a pointer to each element\n"
"// (`for` is explained in the next section)\n"
"for crayons.iter().advance |crayon| {\n"
"foreach crayon in crayons.iter() {\n"
" let delicious_crayon_wax = unwrap_crayon(*crayon);\n"
" eat_crayon_wax(delicious_crayon_wax);\n"
"}\n"
Expand Down Expand Up @@ -3101,7 +3101,7 @@ msgid ""
"~~~~\n"
"fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {\n"
" let mut accumulator = ~[];\n"
" for vector.iter().advance |element| {\n"
" foreach element in vector.iter() {\n"
" accumulator.push(function(element));\n"
" }\n"
" return accumulator;\n"
Expand Down Expand Up @@ -3570,7 +3570,7 @@ msgid ""
"~~~~\n"
"# trait Printable { fn print(&self); }\n"
"fn print_all<T: Printable>(printable_things: ~[T]) {\n"
" for printable_things.iter().advance |thing| {\n"
" foreach thing in printable_things.iter() {\n"
" thing.print();\n"
" }\n"
"}\n"
Expand Down Expand Up @@ -3650,7 +3650,7 @@ msgstr ""
#, no-wrap
msgid ""
"fn draw_all<T: Drawable>(shapes: ~[T]) {\n"
" for shapes.iter().advance |shape| { shape.draw(); }\n"
" foreach shape in shapes.iter() { shape.draw(); }\n"
"}\n"
"# let c: Circle = new_circle();\n"
"# draw_all(~[c]);\n"
Expand All @@ -3673,7 +3673,7 @@ msgid ""
"~~~~\n"
"# trait Drawable { fn draw(&self); }\n"
"fn draw_all(shapes: &[@Drawable]) {\n"
" for shapes.iter().advance |shape| { shape.draw(); }\n"
" foreach shape in shapes.iter() { shape.draw(); }\n"
"}\n"
"~~~~\n"
msgstr ""
Expand Down
6 changes: 3 additions & 3 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -880,11 +880,11 @@ the function name.

~~~~ {.xfail-test}
fn iter<T>(seq: &[T], f: &fn(T)) {
for seq.iter().advance |elt| { f(elt); }
foreach elt in seq.iter() { f(elt); }
}
fn map<T, U>(seq: &[T], f: &fn(T) -> U) -> ~[U] {
let mut acc = ~[];
for seq.iter().advance |elt| { acc.push(f(elt)); }
foreach elt in seq.iter() { acc.push(f(elt)); }
acc
}
~~~~
Expand Down Expand Up @@ -2378,7 +2378,7 @@ An example of a for loop over the contents of a vector:

let v: &[foo] = &[a, b, c];

for v.iter().advance |e| {
foreach e in v.iter() {
bar(*e);
}
~~~~
Expand Down
17 changes: 8 additions & 9 deletions doc/tutorial-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,19 @@ dropped when they become unnecessary.

## For loops

The `for` loop syntax is currently in transition, and will switch from the old
closure-based iteration protocol to iterator objects. For now, the `advance`
adaptor is required as a compatibility shim to use iterators with for loops.
The `foreach` keyword is transitional, and is going to replace the current
obsolete `for` loop.

~~~
let xs = [2, 3, 5, 7, 11, 13, 17];

// print out all the elements in the vector
for xs.iter().advance |x| {
foreach x in xs.iter() {
println(x.to_str())
}

// print out all but the first 3 elements in the vector
for xs.iter().skip(3).advance |x| {
foreach x in xs.iter().skip(3) {
println(x.to_str())
}
~~~
Expand All @@ -193,7 +192,7 @@ let ys = ["foo", "bar", "baz", "foobar"];
let mut it = xs.iter().zip(ys.iter());

// print out the pairs of elements up to (&3, &"baz")
for it.advance |(x, y)| {
foreach (x, y) in it {
printfln!("%d %s", *x, *y);

if *x == 3 {
Expand Down Expand Up @@ -230,7 +229,7 @@ impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
pub fn from_iterator(iterator: &mut T) -> ~[A] {
let (lower, _) = iterator.size_hint();
let mut xs = with_capacity(lower);
for iterator.advance |x| {
foreach x in iterator {
xs.push(x);
}
xs
Expand Down Expand Up @@ -301,7 +300,7 @@ printfln!("%?", it.next()); // prints `Some(&2)`
printfln!("%?", it.next_back()); // prints `Some(&6)`

// prints `5`, `4` and `3`
for it.invert().advance |&x| {
foreach &x in it.invert() {
printfln!("%?", x)
}
~~~
Expand All @@ -320,7 +319,7 @@ let mut it = xs.iter().chain_(ys.iter()).transform(|&x| x * 2);
printfln!("%?", it.next()); // prints `Some(2)`

// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`
for it.invert().advance |x| {
foreach x in it.invert() {
printfln!("%?", x);
}
~~~
Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fn main() {
let mut futures = vec::from_fn(1000, |ind| do extra::future::spawn { partial_sum(ind) });

let mut final_res = 0f64;
for futures.mut_iter().advance |ft| {
foreach ft in futures.mut_iter() {
final_res += ft.get();
}
println(fmt!("π^2/6 is not far from : %?", final_res));
Expand Down
10 changes: 5 additions & 5 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ assert!(!crayons.is_empty());

// Iterate over a vector, obtaining a pointer to each element
// (`for` is explained in the next section)
for crayons.iter().advance |crayon| {
foreach crayon in crayons.iter() {
let delicious_crayon_wax = unwrap_crayon(*crayon);
eat_crayon_wax(delicious_crayon_wax);
}
Expand Down Expand Up @@ -1749,7 +1749,7 @@ of `vector`:
~~~~
fn map<T, U>(vector: &[T], function: &fn(v: &T) -> U) -> ~[U] {
let mut accumulator = ~[];
for vector.iter().advance |element| {
foreach element in vector.iter() {
accumulator.push(function(element));
}
return accumulator;
Expand Down Expand Up @@ -2027,7 +2027,7 @@ generic types.
~~~~
# trait Printable { fn print(&self); }
fn print_all<T: Printable>(printable_things: ~[T]) {
for printable_things.iter().advance |thing| {
foreach thing in printable_things.iter() {
thing.print();
}
}
Expand Down Expand Up @@ -2073,7 +2073,7 @@ However, consider this function:
trait Drawable { fn draw(&self); }

fn draw_all<T: Drawable>(shapes: ~[T]) {
for shapes.iter().advance |shape| { shape.draw(); }
foreach shape in shapes.iter() { shape.draw(); }
}
# let c: Circle = new_circle();
# draw_all(~[c]);
Expand All @@ -2088,7 +2088,7 @@ an _object_.
~~~~
# trait Drawable { fn draw(&self); }
fn draw_all(shapes: &[@Drawable]) {
for shapes.iter().advance |shape| { shape.draw(); }
foreach shape in shapes.iter() { shape.draw(); }
}
~~~~

Expand Down
6 changes: 3 additions & 3 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn make_tests(config: &config) -> ~[test::TestDescAndFn] {
config.src_base.to_str());
let mut tests = ~[];
let dirs = os::list_dir_path(&config.src_base);
for dirs.iter().advance |file| {
foreach file in dirs.iter() {
let file = file.clone();
debug!("inspecting file %s", file.to_str());
if is_test(config, &file) {
Expand Down Expand Up @@ -271,11 +271,11 @@ pub fn is_test(config: &config, testfile: &Path) -> bool {

let mut valid = false;

for valid_extensions.iter().advance |ext| {
foreach ext in valid_extensions.iter() {
if name.ends_with(*ext) { valid = true; }
}

for invalid_prefixes.iter().advance |pre| {
foreach pre in invalid_prefixes.iter() {
if name.starts_with(*pre) { valid = false; }
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiletest/procsrv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn run(lib_path: &str,
err_fd: None
});

for input.iter().advance |input| {
foreach input in input.iter() {
proc.input().write_str(*input);
}
let output = proc.finish_with_output();
Expand Down
18 changes: 9 additions & 9 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
// check if each line in props.check_lines appears in the
// output (in order)
let mut i = 0u;
for ProcRes.stdout.line_iter().advance |line| {
foreach line in ProcRes.stdout.line_iter() {
if check_lines[i].trim() == line.trim() {
i += 1u;
}
Expand Down Expand Up @@ -313,7 +313,7 @@ fn check_error_patterns(props: &TestProps,
let mut next_err_idx = 0u;
let mut next_err_pat = &props.error_patterns[next_err_idx];
let mut done = false;
for ProcRes.stderr.line_iter().advance |line| {
foreach line in ProcRes.stderr.line_iter() {
if line.contains(*next_err_pat) {
debug!("found error pattern %s", *next_err_pat);
next_err_idx += 1u;
Expand All @@ -333,7 +333,7 @@ fn check_error_patterns(props: &TestProps,
fatal_ProcRes(fmt!("error pattern '%s' not found!",
missing_patterns[0]), ProcRes);
} else {
for missing_patterns.iter().advance |pattern| {
foreach pattern in missing_patterns.iter() {
error(fmt!("error pattern '%s' not found!", *pattern));
}
fatal_ProcRes(~"multiple error patterns not found", ProcRes);
Expand Down Expand Up @@ -386,9 +386,9 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
// filename:line1:col1: line2:col2: *warning:* msg
// where line1:col1: is the starting point, line2:col2:
// is the ending point, and * represents ANSI color codes.
for ProcRes.stderr.line_iter().advance |line| {
foreach line in ProcRes.stderr.line_iter() {
let mut was_expected = false;
for expected_errors.iter().enumerate().advance |(i, ee)| {
foreach (i, ee) in expected_errors.iter().enumerate() {
if !found_flags[i] {
debug!("prefix=%s ee.kind=%s ee.msg=%s line=%s",
prefixes[i], ee.kind, ee.msg, line);
Expand Down Expand Up @@ -559,7 +559,7 @@ fn compose_and_run_compiler(
let extra_link_args = ~[~"-L",
aux_output_dir_name(config, testfile).to_str()];

for props.aux_builds.iter().advance |rel_ab| {
foreach rel_ab in props.aux_builds.iter() {
let abs_ab = config.aux_base.push_rel(&Path(*rel_ab));
let aux_args =
make_compile_args(config, props, ~[~"--lib"] + extra_link_args,
Expand Down Expand Up @@ -786,7 +786,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
runargs.push(fmt!("%s", config.adb_test_dir));
runargs.push(fmt!("%s", prog_short));

for args.args.iter().advance |tv| {
foreach tv in args.args.iter() {
runargs.push(tv.to_owned());
}

Expand All @@ -803,7 +803,7 @@ fn _arm_exec_compiled_test(config: &config, props: &TestProps,
Some(~""));

let mut exitcode : int = 0;
for exitcode_out.iter().advance |c| {
foreach c in exitcode_out.iter() {
if !c.is_digit() { break; }
exitcode = exitcode * 10 + match c {
'0' .. '9' => c as int - ('0' as int),
Expand Down Expand Up @@ -852,7 +852,7 @@ fn _arm_push_aux_shared_library(config: &config, testfile: &Path) {
let tstr = aux_output_dir_name(config, testfile).to_str();

let dirs = os::list_dir_path(&Path(tstr));
for dirs.iter().advance |file| {
foreach file in dirs.iter() {

if (file.filetype() == Some(~".so")) {

Expand Down
2 changes: 1 addition & 1 deletion src/libextra/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ mod tests {
}

// Wait for children to pass their asserts
for children.iter().advance |r| {
foreach r in children.iter() {
r.recv();
}

Expand Down
2 changes: 1 addition & 1 deletion src/libextra/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl<'self> FromBase64 for &'self [u8] {
let mut modulus = 0;

let mut it = self.iter();
for it.advance |&byte| {
foreach &byte in it {
let ch = byte as char;
let val = byte as u32;

Expand Down
Loading