1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-20 15:08:02 +02:00

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.
This commit is contained in:
Frances Wingerter 2024-08-13 14:38:22 +00:00 committed by GitHub
parent e9fce0417e
commit dfd08ebf93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 8 additions and 8 deletions

View File

@ -10,7 +10,7 @@ At a high level, async Rust code looks very much like "normal" sequential code:
use futures::executor::block_on; use futures::executor::block_on;
async fn count_to(count: i32) { async fn count_to(count: i32) {
for i in 1..=count { for i in 0..count {
println!("Count is: {i}!"); println!("Count is: {i}!");
} }
} }

View File

@ -10,7 +10,7 @@ Tokio provides:
use tokio::time; use tokio::time;
async fn count_to(count: i32) { async fn count_to(count: i32) {
for i in 1..=count { for i in 0..count {
println!("Count in task: {i}!"); println!("Count in task: {i}!");
time::sleep(time::Duration::from_millis(5)).await; time::sleep(time::Duration::from_millis(5)).await;
} }
@ -20,7 +20,7 @@ async fn count_to(count: i32) {
async fn main() { async fn main() {
tokio::spawn(count_to(10)); tokio::spawn(count_to(10));
for i in 1..5 { for i in 0..5 {
println!("Main task: {i}"); println!("Main task: {i}");
time::sleep(time::Duration::from_millis(5)).await; time::sleep(time::Duration::from_millis(5)).await;
} }

View File

@ -16,7 +16,7 @@ fn main() {
thread::spawn(move || { thread::spawn(move || {
let thread_id = thread::current().id(); let thread_id = thread::current().id();
for i in 1..10 { for i in 0..10 {
tx.send(format!("Message {i}")).unwrap(); tx.send(format!("Message {i}")).unwrap();
println!("{thread_id:?}: sent Message {i}"); println!("{thread_id:?}: sent Message {i}");
} }

View File

@ -16,7 +16,7 @@ fn main() {
thread::spawn(move || { thread::spawn(move || {
let thread_id = thread::current().id(); let thread_id = thread::current().id();
for i in 1..10 { for i in 0..10 {
tx.send(format!("Message {i}")).unwrap(); tx.send(format!("Message {i}")).unwrap();
println!("{thread_id:?}: sent Message {i}"); println!("{thread_id:?}: sent Message {i}");
} }

View File

@ -13,7 +13,7 @@ use std::thread;
fn main() { fn main() {
let v = Arc::new(vec![10, 20, 30]); let v = Arc::new(vec![10, 20, 30]);
let mut handles = Vec::new(); let mut handles = Vec::new();
for _ in 1..5 { for _ in 0..5 {
let v = Arc::clone(&v); let v = Arc::clone(&v);
handles.push(thread::spawn(move || { handles.push(thread::spawn(move || {
let thread_id = thread::current().id(); let thread_id = thread::current().id();

View File

@ -12,13 +12,13 @@ use std::time::Duration;
fn main() { fn main() {
thread::spawn(|| { thread::spawn(|| {
for i in 1..10 { for i in 0..10 {
println!("Count in thread: {i}!"); println!("Count in thread: {i}!");
thread::sleep(Duration::from_millis(5)); thread::sleep(Duration::from_millis(5));
} }
}); });
for i in 1..5 { for i in 0..5 {
println!("Main thread: {i}"); println!("Main thread: {i}");
thread::sleep(Duration::from_millis(5)); thread::sleep(Duration::from_millis(5));
} }