Reputation: 1275
I'm learning Rust by reading codes of Rust applications and I often struggle with finding traits' method implementations for structs.
For instance, I have the following code:
fn peek_u8(src: &mut Cursor<&[u8]>) -> Result<u8, Error> {
if !src.has_remaining() {
return Err(Error::Incomplete);
}
Ok(src.chunk()[0])
}
I want to know where the definition and origin of the 'has_remaining' and 'chunk' method of Cursor. After some Internet research, I found out It originates from 'bytes::Buf' trait. However, I couldn't find that of 'chunk'.
Every time I meet this kind of situations, It is hard to find how struct has methods in it (which trait implements the methods?). Sometime It has very high time costs.
Are there some tips for me?
Upvotes: 5
Views: 413
Reputation: 221
You can usually find this kind of information in the cargo docs for your library or application.
If you are looking at an existing application and you have access to source, you can generate the docs with cargo doc.
In your example you could then go to the cursor and navigate from there or just search for the function name.
Upvotes: 2