Where is the Rust documentation for traits implementation?

I want to know what happens if you do this:

use std::collections::HashMap;

fn main() {
    let mut hm: HashMap<i32, i32> = HashMap::new();
    hm.insert(1, 2);
    let foo = hm[&100]; // Not in the map.
}

      

I think I can find out by testing it, but if I want to find out from the documentation, where does it say this?

If I go to the page HashMap

it has a link for impl Index

which I believe is the relevant code, but that just goes to this general Index

documentation
.

This is not limited HashMap

. I cannot find any documentation for any implementations of traits - only for the traits themselves.

+3


source to share


2 answers


There is no additional documentation for this implementation. A signature is all you get:

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
    where K: Eq + Hash + Borrow<Q>,
          Q: Eq + Hash,
          S: HashState,
{
    type Output = V;

    #[inline]
    fn index(&self, index: &Q) -> &V {
        self.get(index).expect("no entry found for key")
    }
}

      



This is typical, although not particularly good.

+5


source


With this code:

/// A public trait with a method.
pub trait Trait {
    /// This method does something.
    fn method(&self);
}

/// A public structure.
pub struct X;

/// An implementation of `Trait` for `X`.
impl Trait for X {
    /// This method does something specific for `X`.
    fn method(&self) {}
}

      

rustdoc creates the following documentation for X

:

doc page



You can see that the method implementation documentation is lost, but the feature implementation documentation is retained.

So for library authors, the question is whether they write feature injection documentation or not. If they do, you can find them in the traits implementation section; if they don't, well, you can't find it anywhere anyway. In this particular case, the implementation Index

for is HashMap

not documented.

(It might make sense to reopen the rustdoc issue to add the ability to generate documentation for implemented methods from traits, if there is no such issue already.)

+4


source







All Articles