Skip to content

Commit f8a9211

Browse files
committed
Explain modulo in the guide.
Fixes #15954
1 parent 776c17f commit f8a9211

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/doc/guide.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1888,8 +1888,15 @@ fn main() {
18881888

18891889
The first thing we changed was to `use std::rand`, as the docs
18901890
explained. We then added in a `let` expression to create a variable binding
1891-
named `secret_number`, and we printed out its result. Let's try to compile
1892-
this using `cargo build`:
1891+
named `secret_number`, and we printed out its result.
1892+
1893+
Also, you may wonder why we are using `%` on the result of `rand::random()`.
1894+
This operator is called 'modulo', and it returns the remainder of a division.
1895+
By taking the modulo of the result of `rand::random()`, we're limiting the
1896+
values to be between 0 and 99. Then, we add one to the result, making it from 1
1897+
to 100.
1898+
1899+
Let's try to compile this using `cargo build`:
18931900

18941901
```{notrust,no_run}
18951902
$ cargo build

0 commit comments

Comments
 (0)