-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.fixed
100 lines (87 loc) · 2.62 KB
/
main.fixed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// run-rustfix
#[allow(unused_imports)]
use rayon::prelude::*;
// use std::collections::LinkedList;
use std::rc::Rc;
fn main() {
warn_par_iter_simple();
// warn_par_iter_simple_no_send();
warn_par_iter_simple_no_send_in_closure_body();
move_inside_closure();
// warn_par_iter_simple_into_parallel_ref_iterator();
// warn_par_iter();
warn_par_complex();
warn_par_complex_no_send()
}
fn warn_par_iter_simple() {
(0..100).into_par_iter().for_each(|x| println!("{:?}", x));
}
fn move_inside_closure() {
let y = 100;
(0..100)
.into_par_iter()
.for_each(|x| println!("{:?}{:?}", x, y));
}
// fn warn_par_iter_simple_no_send() {
// let list: Vec<Rc<i32>> = (0..100).map(Rc::new).collect();
// list.iter().for_each(|y| println!("{:?}", y));
// }
fn warn_par_iter_simple_no_send_in_closure_body() {
let mut list: Vec<Rc<i32>> = (0..100).map(Rc::new).collect();
(0..100).into_iter().for_each(|x| list.push(Rc::new(x)));
}
// fn warn_par_iter_simple_into_parallel_ref_iterator() {
// let list: LinkedList<i32> = (0..100).collect();
// list.into_iter().for_each(|x| println!("{:?}", x));
// }
struct Person {
name: String,
age: u32,
}
fn warn_par_complex() {
let vec = vec![1, 2, 3, 4, 5, 6];
let a = 10;
let b = 20;
let c = "Hello";
let d = 3.14;
let e = true;
let person = Person {
name: String::from("Alice"),
age: 30,
};
(0..10).into_par_iter().for_each(|x| {
let sum = x + a + b;
let message = if e { c } else { "Goodbye" };
let product = d * (x as f64);
let person_info = format!("{} is {} years old.", person.name, person.age);
println!(
"Sum: {sum}, Message: {message}, Product: {product}, Person: {person_info}, Vec: {vec:?}",
);
});
}
fn warn_par_complex_no_send() {
let a = Rc::new(10);
let b = Rc::new(20);
let c = Rc::new("Hello");
let d = Rc::new(3.14);
let e = Rc::new(true);
let person = Rc::new(Person {
name: String::from("Alice"),
age: 30,
});
(0..10).into_iter().for_each(|x| {
let sum = *a + *b;
let message = if *e { *c } else { "Goodbye" };
let product = *d * (x as f64);
let person_info = format!("{} is {} years old.", person.name, person.age);
println!("Sum: {sum}, Message: {message}, Product: {product}, Person: {person_info}",);
});
}
// struct LocalQueue {}
// fn warn_par_iter() {
// let thread_num = 10;
// let mut locals = Vec::new();
// (0..thread_num).into_iter().for_each(|_| {
// locals.push(LocalQueue {});
// });
// }