Skip to content

Commit dfd08eb

Browse files
authored
concurrency: avoid 1.. loops (#2280)
Fixes #2060. Note that this does not change the "blocking executor" example because on that slide it is worthwhile to sleep for 1 * 10ms on the first iteration and so on. But we shouldn't use one-indexed inclusive loops when the only significant feature of the loop is its iteration count.
1 parent e9fce04 commit dfd08eb

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

src/concurrency/async/async-await.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ At a high level, async Rust code looks very much like "normal" sequential code:
1010
use futures::executor::block_on;
1111
1212
async fn count_to(count: i32) {
13-
for i in 1..=count {
13+
for i in 0..count {
1414
println!("Count is: {i}!");
1515
}
1616
}

src/concurrency/async/runtimes/tokio.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Tokio provides:
1010
use tokio::time;
1111
1212
async fn count_to(count: i32) {
13-
for i in 1..=count {
13+
for i in 0..count {
1414
println!("Count in task: {i}!");
1515
time::sleep(time::Duration::from_millis(5)).await;
1616
}
@@ -20,7 +20,7 @@ async fn count_to(count: i32) {
2020
async fn main() {
2121
tokio::spawn(count_to(10));
2222
23-
for i in 1..5 {
23+
for i in 0..5 {
2424
println!("Main task: {i}");
2525
time::sleep(time::Duration::from_millis(5)).await;
2626
}

src/concurrency/channels/bounded.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
1717
thread::spawn(move || {
1818
let thread_id = thread::current().id();
19-
for i in 1..10 {
19+
for i in 0..10 {
2020
tx.send(format!("Message {i}")).unwrap();
2121
println!("{thread_id:?}: sent Message {i}");
2222
}

src/concurrency/channels/unbounded.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
1717
thread::spawn(move || {
1818
let thread_id = thread::current().id();
19-
for i in 1..10 {
19+
for i in 0..10 {
2020
tx.send(format!("Message {i}")).unwrap();
2121
println!("{thread_id:?}: sent Message {i}");
2222
}

src/concurrency/shared-state/arc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::thread;
1313
fn main() {
1414
let v = Arc::new(vec![10, 20, 30]);
1515
let mut handles = Vec::new();
16-
for _ in 1..5 {
16+
for _ in 0..5 {
1717
let v = Arc::clone(&v);
1818
handles.push(thread::spawn(move || {
1919
let thread_id = thread::current().id();

src/concurrency/threads/plain.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ use std::time::Duration;
1212
1313
fn main() {
1414
thread::spawn(|| {
15-
for i in 1..10 {
15+
for i in 0..10 {
1616
println!("Count in thread: {i}!");
1717
thread::sleep(Duration::from_millis(5));
1818
}
1919
});
2020
21-
for i in 1..5 {
21+
for i in 0..5 {
2222
println!("Main thread: {i}");
2323
thread::sleep(Duration::from_millis(5));
2424
}

0 commit comments

Comments
 (0)