Skip to content

Commit 3e7ab1b

Browse files
author
Johannes Oertel
committed
Update example in "Rust Inside Other Languages"
Use result of the computation to prevent the compiler from optimising too much. Change `_x` to `x` and therefore remove the paragraph about the underscore. Fixes #25855.
1 parent deff2f5 commit 3e7ab1b

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

src/doc/trpl/rust-inside-other-languages.md

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ threads = []
6666
5_000_000.times do
6767
count += 1
6868
end
69+
70+
count
6971
end
7072
end
7173

72-
threads.each { |t| t.join }
74+
threads.each do |t|
75+
puts "Thread finished with count=#{t.value}"
76+
end
7377
puts "done!"
7478
```
7579

@@ -103,50 +107,26 @@ use std::thread;
103107
fn process() {
104108
let handles: Vec<_> = (0..10).map(|_| {
105109
thread::spawn(|| {
106-
let mut _x = 0;
110+
let mut x = 0;
107111
for _ in (0..5_000_000) {
108-
_x += 1
112+
x += 1
109113
}
114+
x
110115
})
111116
}).collect();
112117

113118
for h in handles {
114-
h.join().ok().expect("Could not join a thread!");
119+
println!("Thread finished with count={}",
120+
h.join().map_err(|_| "Could not join a thread!").unwrap());
115121
}
122+
println!("done!");
116123
}
117124
```
118125

119126
Some of this should look familiar from previous examples. We spin up ten
120127
threads, collecting them into a `handles` vector. Inside of each thread, we
121-
loop five million times, and add one to `_x` each time. Why the underscore?
122-
Well, if we remove it and compile:
123-
124-
```bash
125-
$ cargo build
126-
Compiling embed v0.1.0 (file:///home/steve/src/embed)
127-
src/lib.rs:3:1: 16:2 warning: function is never used: `process`, #[warn(dead_code)] on by default
128-
src/lib.rs:3 fn process() {
129-
src/lib.rs:4 let handles: Vec<_> = (0..10).map(|_| {
130-
src/lib.rs:5 thread::spawn(|| {
131-
src/lib.rs:6 let mut x = 0;
132-
src/lib.rs:7 for _ in (0..5_000_000) {
133-
src/lib.rs:8 x += 1
134-
...
135-
src/lib.rs:6:17: 6:22 warning: variable `x` is assigned to, but never used, #[warn(unused_variables)] on by default
136-
src/lib.rs:6 let mut x = 0;
137-
^~~~~
138-
```
139-
140-
That first warning is because we are building a library. If we had a test
141-
for this function, the warning would go away. But for now, it’s never
142-
called.
143-
144-
The second is related to `x` versus `_x`. Because we never actually _do_
145-
anything with `x`, we get a warning about it. In our case, that’s perfectly
146-
okay, as we’re just trying to waste CPU cycles. Prefixing `x` with the
147-
underscore removes the warning.
148-
149-
Finally, we join on each thread.
128+
loop five million times, and add one to `x` each time. Finally, we join on
129+
each thread.
150130

151131
Right now, however, this is a Rust library, and it doesn’t expose anything
152132
that’s callable from C. If we tried to hook this up to another language right

0 commit comments

Comments
 (0)