1. 18
  1.  

  2. 6

    Rust’s docs really need something like the ‘key takeaways’ box from this post or some sort of summary of what the different traits that are available that you’re supposed to use in your own libraries.

    I didn’t know about ‘TryInto’ and even looked for something like it for about 20 minutes. I ended up creating my own ‘AttemptInto’ in a library I started awhile ago because I didn’t know better.

    1. 4

      Where in the docs would such a thing go?

      (And the Try* traits are a very recent addition, so that also may be why you missed them)

      1. 3

        Heh, just got there in the article

        but their exact details and implications are still under debate, which means they’re still marked as unstable.

        I think I just got a little over-excited at first because that was one of the bigger things I had been struggling with how to do it ‘right’. (Before I decided to just make it work first before going back to make it right.)

        I’m not sure where exactly it would go and to be honest I haven’t ventured far beyond the book & std library reference, so it’s possible the group of info I’m looking for exists somewhere and I just haven’t found it yet. Is there a way to get a list of all the standard traits with a short description of each somewhere in the docs?

        1. 4

          No worries :)

          There isn’t, because there’s a lot of them. Such a thing would be really big, and cross-cutting, so it’s unclear of where something like this would go. “Advice for library writers” could be a whole book of its own!

        2. 3

          The top of a particular module may be the place for this sort of summary. So, for example, the std::convert docs page could start with a summary akin to the one seen here, followed by a more in-depth explanation, followed by the actual module contents. The question then is how best to write such a summary box in the doc comments, and what (if any) additions are necessary to Rustdoc to make this look as nice and clear as possible.

          1. 3

            Yes, in general, that’s my approach to module docs. I haven’t gotten to convert yet, so it’s a bit anemic.

            I was reading OP as talking more generally, about all of them. There’s a LOT, and so it would be much less clear to me where it should go; if it were at the top level, it would dominate the rest of the std docs…

            1. 1

              Hmm… a list of traits similar to the list of macros on the standard library API docs home page may be good. It would include in it a list of the traits provided by the standard library, with a short description and link to the full documentation for each.

              1. 2

                This shows off how bad I am at git grep than anything else but…

                $ git grep --perl-regexp "pub trait \w+ {" src | grep -v doc | grep -v test | wc -l
                144
                

                It’d be huuuuuuge.

                1. 3

                  So, a quick skim of the current stable docs finds 78 stable traits. Grouped together into categories, it’s a pretty manageable list. I’ve been tinkering with a crate creation and maintenance guide in my spare time. I may include the type of list and explanation suggested by azdle in there, with a bit about each of these traits.

                  For reference, here are the traits:

                  1. Any
                  2. AsciiExt
                  3. Borrow
                  4. BorrowMut
                  5. ToOwned
                  6. Clone
                  7. Eq
                  8. Ord
                  9. PartialEq
                  10. PartialOrd
                  11. AsMut
                  12. AsRef
                  13. From
                  14. Into
                  15. Default
                  16. Error
                  17. Binary
                  18. Debug
                  19. Display
                  20. LowerExp
                  21. LowerHex
                  22. Octal
                  23. Pointer
                  24. UpperExp
                  25. UpperHex
                  26. fmt::Write
                  27. BuildHasher
                  28. Hash
                  29. Hasher
                  30. BufRead
                  31. Read
                  32. Seek
                  33. io::Write
                  34. DoubleEndedIterator
                  35. ExactSizeIterator
                  36. Extend
                  37. FromIterator
                  38. IntoIterator
                  39. Iterator
                  40. Copy
                  41. Send
                  42. Sized
                  43. Sync
                  44. ToSocketAddrs
                  45. Add
                  46. AddAssign
                  47. BitAnd
                  48. BitAndAssign
                  49. BitOr
                  50. BitOrAssign
                  51. BitXor
                  52. BitXorAssign
                  53. Deref
                  54. DerefMut
                  55. Div
                  56. DivAssign
                  57. Drop
                  58. Fn
                  59. FnMut
                  60. FnOnce
                  61. Index
                  62. IndexMut
                  63. Mul
                  64. MulAssign
                  65. Neg
                  66. Not
                  67. Rem
                  68. RemAssign
                  69. Shl
                  70. ShlAssign
                  71. Shr
                  72. ShrAssign
                  73. Sub
                  74. SubAssign
                  75. RefUnwindSafe
                  76. UnwindSafe
                  77. FromStr
                  78. ToString
                  1. 1

                    Nice list. A quick glance makes me wonder if the list would have value to other languages that do type or proof constraints. Might make a nice reference for what could be useful to include or how one might start at expressing it.

      2. 1

        This is too much syntax candy for my taste. In my opinion, an idiom should be easy to remember, not a list of dozens of “idioms” you have to look in the docs for to find out about.

        1. 6

          This is no syntax candy at all, all of them use the same standard Rust generics syntax. They all work the same, and none of this is an idiom. They are pre-defined conversion types and describe this at a type system level.