提交 919ea473 编写于 作者: S Steve Klabnik

Add main() so that examples work

Rustdoc will automatically wrap things in main, but this doesn't work
here.

Fixes #31249
上级 552bf75e
...@@ -24,6 +24,7 @@ Cargo will automatically generate a simple test when you make a new project. ...@@ -24,6 +24,7 @@ Cargo will automatically generate a simple test when you make a new project.
Here's the contents of `src/lib.rs`: Here's the contents of `src/lib.rs`:
```rust ```rust
# fn main() {}
#[test] #[test]
fn it_works() { fn it_works() {
} }
...@@ -75,6 +76,7 @@ So why does our do-nothing test pass? Any test which doesn't `panic!` passes, ...@@ -75,6 +76,7 @@ So why does our do-nothing test pass? Any test which doesn't `panic!` passes,
and any test that does `panic!` fails. Let's make our test fail: and any test that does `panic!` fails. Let's make our test fail:
```rust ```rust
# fn main() {}
#[test] #[test]
fn it_works() { fn it_works() {
assert!(false); assert!(false);
...@@ -145,6 +147,7 @@ This is useful if you want to integrate `cargo test` into other tooling. ...@@ -145,6 +147,7 @@ This is useful if you want to integrate `cargo test` into other tooling.
We can invert our test's failure with another attribute: `should_panic`: We can invert our test's failure with another attribute: `should_panic`:
```rust ```rust
# fn main() {}
#[test] #[test]
#[should_panic] #[should_panic]
fn it_works() { fn it_works() {
...@@ -175,6 +178,7 @@ Rust provides another macro, `assert_eq!`, that compares two arguments for ...@@ -175,6 +178,7 @@ Rust provides another macro, `assert_eq!`, that compares two arguments for
equality: equality:
```rust ```rust
# fn main() {}
#[test] #[test]
#[should_panic] #[should_panic]
fn it_works() { fn it_works() {
...@@ -209,6 +213,7 @@ make sure that the failure message contains the provided text. A safer version ...@@ -209,6 +213,7 @@ make sure that the failure message contains the provided text. A safer version
of the example above would be: of the example above would be:
```rust ```rust
# fn main() {}
#[test] #[test]
#[should_panic(expected = "assertion failed")] #[should_panic(expected = "assertion failed")]
fn it_works() { fn it_works() {
...@@ -219,6 +224,7 @@ fn it_works() { ...@@ -219,6 +224,7 @@ fn it_works() {
That's all there is to the basics! Let's write one 'real' test: That's all there is to the basics! Let's write one 'real' test:
```rust,ignore ```rust,ignore
# fn main() {}
pub fn add_two(a: i32) -> i32 { pub fn add_two(a: i32) -> i32 {
a + 2 a + 2
} }
...@@ -238,6 +244,7 @@ Sometimes a few specific tests can be very time-consuming to execute. These ...@@ -238,6 +244,7 @@ Sometimes a few specific tests can be very time-consuming to execute. These
can be disabled by default by using the `ignore` attribute: can be disabled by default by using the `ignore` attribute:
```rust ```rust
# fn main() {}
#[test] #[test]
fn it_works() { fn it_works() {
assert_eq!(4, add_two(2)); assert_eq!(4, add_two(2));
...@@ -299,6 +306,7 @@ missing the `tests` module. The idiomatic way of writing our example ...@@ -299,6 +306,7 @@ missing the `tests` module. The idiomatic way of writing our example
looks like this: looks like this:
```rust,ignore ```rust,ignore
# fn main() {}
pub fn add_two(a: i32) -> i32 { pub fn add_two(a: i32) -> i32 {
a + 2 a + 2
} }
...@@ -327,6 +335,7 @@ a large module, and so this is a common use of globs. Let's change our ...@@ -327,6 +335,7 @@ a large module, and so this is a common use of globs. Let's change our
`src/lib.rs` to make use of it: `src/lib.rs` to make use of it:
```rust,ignore ```rust,ignore
# fn main() {}
pub fn add_two(a: i32) -> i32 { pub fn add_two(a: i32) -> i32 {
a + 2 a + 2
} }
...@@ -377,6 +386,7 @@ put a `tests/lib.rs` file inside, with this as its contents: ...@@ -377,6 +386,7 @@ put a `tests/lib.rs` file inside, with this as its contents:
```rust,ignore ```rust,ignore
extern crate adder; extern crate adder;
# fn main() {}
#[test] #[test]
fn it_works() { fn it_works() {
assert_eq!(4, adder::add_two(2)); assert_eq!(4, adder::add_two(2));
...@@ -432,6 +442,7 @@ running examples in your documentation (**note:** this only works in library ...@@ -432,6 +442,7 @@ running examples in your documentation (**note:** this only works in library
crates, not binary crates). Here's a fleshed-out `src/lib.rs` with examples: crates, not binary crates). Here's a fleshed-out `src/lib.rs` with examples:
```rust,ignore ```rust,ignore
# fn main() {}
//! The `adder` crate provides functions that add numbers to other numbers. //! The `adder` crate provides functions that add numbers to other numbers.
//! //!
//! # Examples //! # Examples
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册