Skip to content

Commit 49018c0

Browse files
committed
trpl: Proofread "Rust Inside Other Languages"
1 parent 9bebe5f commit 49018c0

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* [Learn Rust](learn-rust.md)
88
* [Guessing Game](guessing-game.md)
99
* [Dining Philosophers](dining-philosophers.md)
10-
* [Rust inside other languages](rust-inside-other-languages.md)
10+
* [Rust Inside Other Languages](rust-inside-other-languages.md)
1111
* [Effective Rust](effective-rust.md)
1212
* [The Stack and the Heap](the-stack-and-the-heap.md)
1313
* [Testing](testing.md)

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ Rust’s greatest strengths: a lack of a substantial runtime.
66
As organizations grow, they increasingly rely on a multitude of programming
77
languages. Different programming languages have different strengths and
88
weaknesses, and a polyglot stack lets you use a particular language where
9-
its strengths make sense, and use a different language where it’s weak.
9+
its strengths make sense and a different one where it’s weak.
1010

1111
A very common area where many programming languages are weak is in runtime
1212
performance of programs. Often, using a language that is slower, but offers
13-
greater programmer productivity is a worthwhile trade-off. To help mitigate
14-
this, they provide a way to write some of your system in C, and then call
15-
the C code as though it were written in the higher-level language. This is
13+
greater programmer productivity, is a worthwhile trade-off. To help mitigate
14+
this, they provide a way to write some of your system in C and then call
15+
that C code as though it were written in the higher-level language. This is
1616
called a ‘foreign function interface’, often shortened to ‘FFI’.
1717

1818
Rust has support for FFI in both directions: it can call into C code easily,
1919
but crucially, it can also be called _into_ as easily as C. Combined with
2020
Rust’s lack of a garbage collector and low runtime requirements, this makes
2121
Rust a great candidate to embed inside of other languages when you need
22-
some extra oomph.
22+
that extra oomph.
2323

2424
There is a whole [chapter devoted to FFI][ffi] and its specifics elsewhere in
25-
the book, but in this chapter, we’ll examine this particular use-case of FFI,
26-
with three examples, in Ruby, Python, and JavaScript.
25+
the book, but in this chapter, we’ll examine this particular use case of FFI,
26+
with examples in Ruby, Python, and JavaScript.
2727

2828
[ffi]: ffi.html
2929

@@ -40,18 +40,18 @@ optimizations can stack allocate particular numbers, but rather than relying
4040
on an optimizer to do its job, we may want to ensure that we’re always using
4141
primitive number types rather than some sort of object type.
4242

43-
Second, many languages have a ‘global interpreter lock’, which limits
43+
Second, many languages have a ‘global interpreter lock’ (GIL), which limits
4444
concurrency in many situations. This is done in the name of safety, which is
4545
a positive effect, but it limits the amount of work that can be done at the
4646
same time, which is a big negative.
4747

4848
To emphasize these two aspects, we’re going to create a little project that
49-
uses these two aspects heavily. Since the focus of the example is the embedding
50-
of Rust into the languages, rather than the problem itself, we’ll just use a
49+
uses these two aspects heavily. Since the focus of the example is to embed
50+
Rust into other languages, rather than the problem itself, we’ll just use a
5151
toy example:
5252

5353
> Start ten threads. Inside each thread, count from one to five million. After
54-
> All ten threads are finished, print out ‘done!’.
54+
> all ten threads are finished, print out ‘done!’.
5555
5656
I chose five million based on my particular computer. Here’s an example of this
5757
code in Ruby:
@@ -69,7 +69,7 @@ threads = []
6969
end
7070
end
7171

72-
threads.each {|t| t.join }
72+
threads.each { |t| t.join }
7373
puts "done!"
7474
```
7575

@@ -82,12 +82,12 @@ sort of process monitoring tool, like `top`, I can see that it only uses one
8282
core on my machine. That’s the GIL kicking in.
8383

8484
While it’s true that this is a synthetic program, one can imagine many problems
85-
that are similar to this in the real world. For our purposes, spinning up some
85+
that are similar to this in the real world. For our purposes, spinning up a few
8686
busy threads represents some sort of parallel, expensive computation.
8787

8888
# A Rust library
8989

90-
Let’s re-write this problem in Rust. First, let’s make a new project with
90+
Let’s rewrite this problem in Rust. First, let’s make a new project with
9191
Cargo:
9292

9393
```bash
@@ -104,7 +104,7 @@ fn process() {
104104
let handles: Vec<_> = (0..10).map(|_| {
105105
thread::spawn(|| {
106106
let mut _x = 0;
107-
for _ in (0..5_000_001) {
107+
for _ in (0..5_000_000) {
108108
_x += 1
109109
}
110110
})
@@ -129,7 +129,7 @@ src/lib.rs:3 fn process() {
129129
src/lib.rs:4 let handles: Vec<_> = (0..10).map(|_| {
130130
src/lib.rs:5 thread::spawn(|| {
131131
src/lib.rs:6 let mut x = 0;
132-
src/lib.rs:7 for _ in (0..5_000_001) {
132+
src/lib.rs:7 for _ in (0..5_000_000) {
133133
src/lib.rs:8 x += 1
134134
...
135135
src/lib.rs:6:17: 6:22 warning: variable `x` is assigned to, but never used, #[warn(unused_variables)] on by default
@@ -151,7 +151,7 @@ Finally, we join on each thread.
151151
Right now, however, this is a Rust library, and it doesn’t expose anything
152152
that’s callable from C. If we tried to hook this up to another language right
153153
now, it wouldn’t work. We only need to make two small changes to fix this,
154-
though. The first is modify the beginning of our code:
154+
though. The first is to modify the beginning of our code:
155155
156156
```rust,ignore
157157
#[no_mangle]
@@ -161,7 +161,7 @@ pub extern fn process() {
161161
We have to add a new attribute, `no_mangle`. When you create a Rust library, it
162162
changes the name of the function in the compiled output. The reasons for this
163163
are outside the scope of this tutorial, but in order for other languages to
164-
know how to call the function, we need to not do that. This attribute turns
164+
know how to call the function, we can’t do that. This attribute turns
165165
that behavior off.
166166
167167
The other change is the `pub extern`. The `pub` means that this function should
@@ -178,7 +178,7 @@ crate-type = ["dylib"]
178178
```
179179
180180
This tells Rust that we want to compile our library into a standard dynamic
181-
library. By default, Rust compiles into an ‘rlib’, a Rust-specific format.
181+
library. By default, Rust compiles an ‘rlib’, a Rust-specific format.
182182
183183
Let’s build the project now:
184184
@@ -204,7 +204,7 @@ Now that we’ve got our Rust library built, let’s use it from our Ruby.
204204
205205
# Ruby
206206
207-
Open up a `embed.rb` file inside of our project, and do this:
207+
Open up an `embed.rb` file inside of our project, and do this:
208208
209209
```ruby
210210
require 'ffi'
@@ -217,7 +217,7 @@ end
217217

218218
Hello.process
219219

220-
puts "done!
220+
puts 'done!'
221221
```
222222
223223
Before we can run this, we need to install the `ffi` gem:
@@ -241,7 +241,7 @@ done!
241241
$
242242
```
243243
244-
Whoah, that was fast! On my system, this took `0.086` seconds, rather than
244+
Whoa, that was fast! On my system, this took `0.086` seconds, rather than
245245
the two seconds the pure Ruby version took. Let’s break down this Ruby
246246
code:
247247
@@ -258,11 +258,11 @@ module Hello
258258
ffi_lib 'target/release/libembed.so'
259259
```
260260
261-
The `ffi` gem’s authors recommend using a module to scope the functions
262-
we’ll import from the shared library. Inside, we `extend` the necessary
263-
`FFI::Library` module, and then call `ffi_lib` to load up our shared
264-
object library. We just pass it the path that our library is stored,
265-
which as we saw before, is `target/release/libembed.so`.
261+
The `Hello` module is used to attach the native functions from the shared
262+
library. Inside, we `extend` the necessary `FFI::Library` module and then call
263+
`ffi_lib` to load up our shared object library. We just pass it the path that
264+
our library is stored, which, as we saw before, is
265+
`target/release/libembed.so`.
266266
267267
```ruby
268268
attach_function :process, [], :void
@@ -280,10 +280,10 @@ Hello.process
280280
281281
This is the actual call into Rust. The combination of our `module`
282282
and the call to `attach_function` sets this all up. It looks like
283-
a Ruby function, but is actually Rust!
283+
a Ruby function but is actually Rust!
284284
285285
```ruby
286-
puts "done!"
286+
puts 'done!'
287287
```
288288
289289
Finally, as per our project’s requirements, we print out `done!`.
@@ -329,7 +329,7 @@ After that installs, we can use it:
329329
var ffi = require('ffi');
330330

331331
var lib = ffi.Library('target/release/libembed', {
332-
'process': [ 'void', [] ]
332+
'process': ['void', []]
333333
});
334334

335335
lib.process();
@@ -340,7 +340,7 @@ console.log("done!");
340340
It looks more like the Ruby example than the Python example. We use
341341
the `ffi` module to get access to `ffi.Library()`, which loads up
342342
our shared object. We need to annotate the return type and argument
343-
types of the function, which are 'void' for return, and an empty
343+
types of the function, which are `void` for return and an empty
344344
array to signify no arguments. From there, we just call it and
345345
print the result.
346346

0 commit comments

Comments
 (0)