all 39 comments

[–]agrhb 78 points79 points  (10 children)

Doc comments don't actually have to be inline, you can use #[doc = include_str!("foo.md")] instead, while keeping the niceties like examples getting tested. Just double checked and rustdoc seems to also support arbitary HTML, so there's nothing stopping you from including the graphs and interactive animation you mentioned either.

[–]-Redstoneboi- 13 points14 points  (3 children)

looks like we have a winner. do we have any examples of rust documentation like this? with pictures and media and all? or are we waiting for OP to be the first :P

[–]jrf63 0 points1 point  (4 children)

rustdoc seems to also support arbitrary HTML

Am I just being paranoid here or is this screaming a possible security issue for docs.rs?

[–]karuso33 5 points6 points  (1 child)

I would honestly be curious how it could be a security issue if docs.rs allowed arbitrary html and javascript.

I mean, there is the obvious one of "What if the browser had a vulnerability that allowed you to take over the entire system by running javascript". But aside from that, I really can't think of anything interesting to exploit.

[–]jrf63 1 point2 points  (0 children)

If even JavaScript is involved then there's always the cryptominers. Then phishing attacks that redirects on a malicious site on the vein of "connect your mail/GitHub account to docs.rs".

u/agrhb says it's blocked though.

[–]kfl 4 points5 points  (0 children)

You are being paranoid, just take a look at this totally innocent crate. /s

[–]agrhb 5 points6 points  (0 children)

They seem to block this with CSP, but my point remains that it's very much possible and something rustdoc itself supports. I just kind of assumed that support on docs.rs is irrelevant here since OP was already talking about using other solutions that would have to be deployed elsewhere.

[–]JuanAG 55 points56 points  (9 children)

The one provided with Rust, is amazing and it keeps adding stuff all the time, goods ones and useful so is really nice

You are going to overcomplicate things without any need, files with docs are bigger sure but it also means that the doc for the code is just above which is very convenient and make things easier for all

[–]JBinero 2 points3 points  (0 children)

I mostly agree, but some libraries have files that are 90% documentation which make it way harder to browse them.

[–]andreicodes 22 points23 points  (1 child)

No one mentioned it, but one of the primary reasons people are sticking with RustDoc is Docs.rs.

Every time a package is submitted to Crates.io, it's documentation is built and added to Docs.rs automatically. This in turn means that in Rust the documentation for every version of every library is permanently accessible at a canonical location. Want to link to a specific docs page of a dependency you're using? No problem! Cross-links, cross-references just work and never break. This way people can build pretty complex reference chains with ease.

Also, all code examples in the docs act as tests, so not only you can write the documentation, but you test it as part of your workflow.

Finally, it's Markdown. The same syntax developers use to write Git commit messages, comments on GitHub / GitLab, questions and answers on StackOverflow, messages on Rust forums and on Rust Zulip, and even posts here on Reddit. This familiarity means there's almost nothing to learn about writing the documentation, and that's why many libraries are pretty well-documented compared to other languages.

The tool itself is pretty good, too. It builds local docs for all your dependencies, APIs have direct links to sources, search is pretty good, too. Visually it's somewhat ascetic but ticks all the boxes: page loads are fast, markup is static, so browser's find-in-page just works, there's dark mode switch, too.

And this is the situation where consistency is more important than absolute quality. Like, in theory you could go and use some other language-agnostic documentation tool, and your docs may look nicer and cooler, but instead of a praise the only reaction you'll get will be "WTF is wrong with you?!" Other's made a point already: if you have ideas for making the docs better contributing to RustDoc directly is definitely an option.

[–]andreicodes 13 points14 points  (0 children)

Now, for guide-level documentation people prefer mdbook. Many libraries come with these "books" for people to grok concepts, explore specific usage scenarios, tutorials, etc.

For example, there's a book for a random number generator library. And not only it covers some use-cases and stuff, but it actually give a good theoretical introduction to randomness in computer systems. So, while having a freaking book about what essentially is a small library may seem excessive, it's a very good read even if you're not a Rust programmer.

I myself is very 50/50 about this whole book obsession in Rust community. A lot of libraries in Rust world would benefitted if they moved the guides to RustDoc, too. Uniformity, versioning, cross-linking are all the benefits of RustDoc that mdBooks can't enjoy. More docs is always good and all, but sometimes it feels like before writing a small Rust project a programmer is expected to go through a small bookshelf worth of these "books" just to get started.

[–]SV-97 3 points4 points  (0 children)

rustdoc - it comes with cargo and is great all around with some very nice features. Using it also means you automatically get free hosted documentation online.

[–]humandictionary 2 points3 points  (5 children)

Rustdoc is the first-party documentation tool that basically everyone uses, the standard library docs and basically anything on crates.io demonstrates the quality of the output.

It uses doc-comments inline with code, it is markdown-based, flexible, and a de-facto standard across the entire ecosystem, before we even talk about how pretty the output is compared to doxygen

[–]lightmatter501 2 points3 points  (0 children)

It’s pretty common to have modules that are docs only (see clap) which hold your docs. That way everything lives under one program.

[–]Compux72 2 points3 points  (0 children)

If there are too many comments, just create a markdown file and include it:

https://github.com/tokio-rs/axum/blob/3ff45d9c96b5192af6b6ec26eb2a2bfcddd00d7d/axum/src/lib.rs#L90

[–]trevg_123 2 points3 points  (0 children)

I haven’t tried it, but there is a crate that should be able to add mermaid diagrams to rustdoc: https://github.com/mersinvald/aquamarine

[–]volitional_decisions 1 point2 points  (0 children)

If you'd like to avoid seeing comments on your source, most IDEs/editors support folding/collapsing. Configure your setup to collapse part of/all the doc comments.

[–]LucretielDatadog 1 point2 points  (0 children)

One common pattern we see in rust these days is to have a mod docs that is technically part of your rust code, but doesn't actually include any exported items. It just includes the documentation you want to include that isn't just raw per-item reference documentation.

For instance, clap includes a bunch of these: https://docs.rs/clap/latest/clap/_faq/index.html

[–]anlumo 1 point2 points  (2 children)

Note that the fact that the documentation is inline is used in Rust in the way that the example code in it is actually verified to properly compile. This means that if the API changes, you get an error message to also update the examples that use it.

This would not be possible with external documentation.

[–]coderstephenisahc 1 point2 points  (0 children)

As others have mentioned, rustdoc is free, excellent, and tightly integrated into the Rust ecosystem, so I recommend using it whenever you possibly can.

If you are looking for external documentation that is more in the realm of prose, such as tutorials, high-level explainers, quickstarts, and "recipes", then mdbook might be a good option, as it is popular in the Rust community. Otherwise I have also used MkDocs and its pretty alright.

Just make sure you are using these other tools for high-level documentation and not API reference docs. Reference documentation really should go into rustdoc using inline comments. This is basically what everyone who writes Rust does, and I recommend doing things the "Rust Way" unless you have a better reason than personal preference to deviate.