rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.98k stars 363 forks source link

Pattern for writing rustdoc examples without initialization #91

Closed tkaitchuck closed 3 years ago

tkaitchuck commented 4 years ago

If you have a type Foo that has some complex initialization that you don't really want to inline into each example for every method on Foo, you can instead do:

/// bar is an important method on Foo. It does things.
/// #Example
/// ```
/// # fn call_bar(foo: &Foo) {
/// let value = foo.bar();
/// # }
/// ```
fn bar(&self) -> u32 {
//...
}

This avoids having to type several lines of logic to initialize foo in a concise way without having to jump to doing a "```ignore".

pickfire commented 4 years ago

Ah, I didn't know this works. Interested to send a patch for this?

tkaitchuck commented 4 years ago

https://github.com/rust-unofficial/patterns/pull/97

simonsan commented 3 years ago

Should be fixed. Closing.