1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-02 23:57:25 +02:00
Andrew Walbran 8f6dcf35f5 Fix Rustdoc.
2023-01-17 16:07:10 +00:00

21 lines
700 B
Markdown

# Documentation Tests
Rust has built-in support for documentation tests:
```rust
/// Shortens a string to the given length.
///
/// ```
/// use playground::shorten_string;
/// assert_eq!(shorten_string("Hello World", 5), "Hello");
/// assert_eq!(shorten_string("Hello World", 20), "Hello World");
/// ```
pub fn shorten_string(s: &str, length: usize) -> &str {
&s[..std::cmp::min(length, s.len())]
}
```
* Code blocks in `///` comments are automatically seen as Rust code.
* The code will be compiled and executed as part of `cargo test`.
* Test the above code on the [Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3ce2ad13ea1302f6572cb15cd96becf0).