Auto merge of #27994 - steveklabnik:rollup, r=steveklabnik
- Successful merges: #27905, #27968, #27978, #27982, #27988 - Failed merges:
This commit is contained in:
commit
1806174ab4
4 changed files with 72 additions and 9 deletions
|
|
@ -1452,7 +1452,7 @@ fn draw_twice<T: Shape>(surface: Surface, sh: T) {
|
|||
}
|
||||
```
|
||||
|
||||
Traits also define an [trait object](#trait-objects) with the same
|
||||
Traits also define a [trait object](#trait-objects) with the same
|
||||
name as the trait. Values of this type are created by coercing from a
|
||||
pointer of some specific type to a pointer of trait type. For example,
|
||||
`&T` could be coerced to `&Shape` if `T: Shape` holds (and similarly
|
||||
|
|
|
|||
|
|
@ -208,8 +208,8 @@ Because these kinds of situations are relatively rare, use panics sparingly.
|
|||
|
||||
In certain circumstances, even though a function may fail, we may want to treat
|
||||
it as a panic instead. For example, `io::stdin().read_line(&mut buffer)` returns
|
||||
a `Result<usize>`, when there is an error reading the line. This allows us to
|
||||
handle and possibly recover from error.
|
||||
a `Result<usize>`, which can indicate an error if one occurs when reading the line.
|
||||
This allows us to handle and possibly recover from errors.
|
||||
|
||||
If we don't want to handle this error, and would rather just abort the program,
|
||||
we can use the `unwrap()` method:
|
||||
|
|
|
|||
|
|
@ -219,6 +219,66 @@ fn it_works() {
|
|||
This is a very common use of `assert_eq!`: call some function with
|
||||
some known arguments and compare it to the expected output.
|
||||
|
||||
# The `ignore` attribute
|
||||
|
||||
Sometimes a few specific tests can be very time-consuming to execute. These
|
||||
can be disabled by default by using the `ignore` attribute:
|
||||
|
||||
```rust
|
||||
#[test]
|
||||
fn it_works() {
|
||||
assert_eq!(4, add_two(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn expensive_test() {
|
||||
// code that takes an hour to run
|
||||
}
|
||||
```
|
||||
|
||||
Now we run our tests and see that `it_works` is run, but `expensive_test` is
|
||||
not:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
Compiling adder v0.0.1 (file:///home/you/projects/adder)
|
||||
Running target/adder-91b3e234d4ed382a
|
||||
|
||||
running 2 tests
|
||||
test expensive_test ... ignored
|
||||
test it_works ... ok
|
||||
|
||||
test result: ok. 1 passed; 0 failed; 1 ignored; 0 measured
|
||||
|
||||
Doc-tests adder
|
||||
|
||||
running 0 tests
|
||||
|
||||
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
|
||||
```
|
||||
|
||||
The expensive tests can be run explicitly using `cargo test -- --ignored`:
|
||||
|
||||
```bash
|
||||
$ cargo test -- --ignored
|
||||
Running target/adder-91b3e234d4ed382a
|
||||
|
||||
running 1 test
|
||||
test expensive_test ... ok
|
||||
|
||||
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
|
||||
|
||||
Doc-tests adder
|
||||
|
||||
running 0 tests
|
||||
|
||||
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
|
||||
```
|
||||
|
||||
The `--ignored` argument is an argument to the test binary, and not to cargo,
|
||||
which is why the command is `cargo test -- --ignored`.
|
||||
|
||||
# The `tests` module
|
||||
|
||||
There is one way in which our existing example is not idiomatic: it's
|
||||
|
|
|
|||
|
|
@ -731,9 +731,14 @@ type X = u32; // ok!
|
|||
"##,
|
||||
|
||||
E0133: r##"
|
||||
Using unsafe functionality, such as dereferencing raw pointers and calling
|
||||
functions via FFI or marked as unsafe, is potentially dangerous and disallowed
|
||||
by safety checks. These safety checks can be relaxed for a section of the code
|
||||
Using unsafe functionality, is potentially dangerous and disallowed
|
||||
by safety checks. Examples:
|
||||
|
||||
- Dereferencing raw pointers
|
||||
- Calling functions via FFI
|
||||
- Calling functions marked unsafe
|
||||
|
||||
These safety checks can be relaxed for a section of the code
|
||||
by wrapping the unsafe instructions with an `unsafe` block. For instance:
|
||||
|
||||
```
|
||||
|
|
@ -831,9 +836,7 @@ is a size mismatch in one of the impls.
|
|||
It is also possible to manually transmute:
|
||||
|
||||
```
|
||||
let result: SomeType = mem::uninitialized();
|
||||
unsafe { copy_nonoverlapping(&v, &result) };
|
||||
result // `v` transmuted to type `SomeType`
|
||||
ptr::read(&v as *const _ as *const SomeType) // `v` transmuted to `SomeType`
|
||||
```
|
||||
"##,
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue