2022-12-21 16:36:30 +01:00
# Documentation Tests
Rust has built-in support for documentation tests:
```rust
2023-01-17 12:33:50 +00:00
/// Shortens a string to the given length.
2022-12-21 16:36:30 +01:00
///
/// ```
/// 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` .
2023-01-17 12:33:50 +00:00
* Test the above code on the [Rust Playground ](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3ce2ad13ea1302f6572cb15cd96becf0 ).