-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #96015 - Dylan-DPC:rollup-vhdprid, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #93217 (Improve Rustdoc UI for scraped examples with multiline arguments, fix overflow in line numbers) - #95885 (Improve error message in case of missing checksum) - #95962 (Document that DirEntry holds the directory open) - #95991 (fix: wrong trait import suggestion for T:) - #96005 (Add missing article to fix "few" to "a few".) - #96006 (Add a missing article) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
- Loading branch information
Showing
19 changed files
with
391 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Scraped examples | ||
|
||
Rustdoc has an unstable feature where it can automatically scrape examples of items being documented from the `examples/` directory of a Cargo workspace. These examples will be included within the generated documentation for that item. For example, if your library contains a public function: | ||
|
||
```rust,ignore (needs-other-file) | ||
// a_crate/src/lib.rs | ||
pub fn a_func() {} | ||
``` | ||
|
||
And you have an example calling this function: | ||
|
||
```rust,ignore (needs-other-file) | ||
// a_crate/examples/ex.rs | ||
fn main() { | ||
a_crate::a_func(); | ||
} | ||
``` | ||
|
||
Then this code snippet will be included in the documentation for `a_func`. This documentation is inserted by Rustdoc and cannot be manually edited by the crate author. | ||
|
||
|
||
## How to use this feature | ||
|
||
This feature is unstable, so you can enable it by calling Rustdoc with the unstable `rustdoc-scrape-examples` flag: | ||
|
||
```bash | ||
cargo doc -Zunstable-options -Zrustdoc-scrape-examples=examples | ||
``` | ||
|
||
To enable this feature on [docs.rs](https://docs.rs), add this to your Cargo.toml: | ||
|
||
```toml | ||
[package.metadata.docs.rs] | ||
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples=examples"] | ||
``` | ||
|
||
|
||
## How it works | ||
|
||
When you run `cargo doc`, Rustdoc will analyze all the crates that match Cargo's `--examples` filter for instances of items being documented. Then Rustdoc will include the source code of these instances in the generated documentation. | ||
|
||
Rustdoc has a few techniques to ensure these examples don't overwhelm documentation readers, and that it doesn't blow up the page size: | ||
|
||
1. For a given item, a maximum of 5 examples are included in the page. The remaining examples are just links to source code. | ||
2. Only one example is shown by default, and the remaining examples are hidden behind a toggle. | ||
3. For a given file that contains examples, only the item containing the examples will be included in the generated documentation. | ||
|
||
For a given item, Rustdoc sorts its examples based on the size of the example — smaller ones are shown first. | ||
|
||
|
||
## FAQ | ||
|
||
### My example is not showing up in the documentation | ||
|
||
This feature uses Cargo's convention for finding examples. You should ensure that `cargo check --examples` includes your example file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.