@ -39,7 +39,7 @@ look right, but you _will_ still be able to build the book. To use the plugins,
@@ -39,7 +39,7 @@ look right, but you _will_ still be able to build the book. To use the plugins,
@ -62,6 +62,9 @@ cd ../../..
@@ -62,6 +62,9 @@ cd ../../..
</Listing>
The file starts with an example `add` function, so that we have something
to test.
For now, let’s focus solely on the `it_works` function. Note the `#[test]`
annotation: this attribute indicates this is a test function, so the test
runner knows to treat this function as a test. We might also have non-test
@ -69,9 +72,9 @@ functions in the `tests` module to help set up common scenarios or perform
@@ -69,9 +72,9 @@ functions in the `tests` module to help set up common scenarios or perform
common operations, so we always need to indicate which functions are tests.
The example function body uses the `assert_eq!` macro to assert that `result`,
which contains the result of adding 2 and 2, equals 4. This assertion serves as
an example of the format for a typical test. Let’s run it to see that this test
passes.
which contains the result of calling `add` with 2 and 2, equals 4. This
assertion serves as an example of the format for a typical test. Let’s run it
to see that this test passes.
The `cargo test` command runs all tests in our project, as shown in Listing
@ -186,9 +186,14 @@ With the `unsafe` block, we’re asserting to Rust that we’ve read the functio
@@ -186,9 +186,14 @@ With the `unsafe` block, we’re asserting to Rust that we’ve read the functio
documentation, we understand how to use it properly, and we’ve verified that
we’re fulfilling the contract of the function.
Bodies of unsafe functions are effectively `unsafe` blocks, so to perform other
unsafe operations within an unsafe function, we don’t need to add another
`unsafe` block.
> Note: In earlier versions of Rust, the body of an unsafe function was treated
> as an `unsafe` block, so you could perform any unsafe operation within the
> body of an `unsafe` function. In later versions of Rust, the compiler will
> warn you that you need to use an `unsafe` block to perform unsafe operations
> in the body of an unsafe function. This is because Rust now distinguishes
> between `unsafe fn`, which defines what you need to do to call the function
> safely, and an `unsafe` block, where you actually uphold that “contract” the
@ -95,33 +95,33 @@ function. However, you can’t do that with closures because they don’t have a
@@ -95,33 +95,33 @@ function. However, you can’t do that with closures because they don’t have a
concrete type that is returnable; you’re not allowed to use the function
pointer `fn` as a return type, for example.
The following code tries to return a closure directly, but it won’t compile:
Instead, you will normally use the `impl Trait` syntax we learned about in
Chapter 10. You can return any function type, using `Fn`, `FnOnce` and `FnMut`.