# After 3 months, still learning to read Rust code

**URL:** <https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616>\
**Category:** help\
**Created:** [February 14, 2021, 11:16pm UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616 "2021-02-14T23:16:32Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mvolkmann/32/18387_2.png) [@mvolkmann](https://users.rust-lang.org/u/mvolkmann)\
**Post date:** [February 14, 2021, 11:16pm UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/1 "2021-02-14T23:16:33Z")

</div>

I'm going through the tokio tutorial. Very early on I encountered this line of code:

```rust
    client.set("hello", "world".into()).await?;

```

I'm wondering about the `"world".into()` part. I know `"world"` has the type `&str`. My first thought is that the `str` type must implement the `Into` trait. Maybe it turns the string into an array of bytes. To verify this I went here: [str - Rust](https://doc.rust-lang.org/std/primitive.str.html). Scrolling down the left nav I find the section that lists the traits this type implements, but `Into` is not there. Then I looked through the list of methods and `into` is not there.

I feel like I'm missing some fundamental understanding about how to read Rust code. What am I missing?

---

<div class="post-metadata">

**Author:** ![Rustaceous](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@Rustaceous](https://users.rust-lang.org/u/Rustaceous)\
**Post date:** [February 15, 2021, 12:00am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/2 "2021-02-15T00:00:11Z")

</div>

As the `Into` docs say,

> One should avoid implementing [`Into`](https://doc.rust-lang.org/std/convert/trait.Into.html) and implement [`From`](https://doc.rust-lang.org/std/convert/trait.From.html) instead. Implementing [`From`](https://doc.rust-lang.org/std/convert/trait.From.html) automatically provides one with an implementation of [`Into`](https://doc.rust-lang.org/std/convert/trait.Into.html) thanks to the blanket implementation in the standard library.

So some other type has `impl<'a> From<&'a str> for Something {...}`.

The problem is one of code quality. `Into` gives the compiler plenty of information because of type inference but it doesn't give a human as much information as it could. I prefer to be as explicit as possible. To that end I never call the `into` method and instead make the conversion obvious to the reader. You would have to look at the function signature to know what type "world" is being converted into. Reasonable options are String, a String newtype and some specialized string types like PathBuf.

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mvolkmann/32/18387_2.png) [@mvolkmann](https://users.rust-lang.org/u/mvolkmann)\
**Post date:** [February 15, 2021, 12:43am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/3 "2021-02-15T00:43:04Z")

</div>

It seems what I failed to recognize is that I needed to start by determining the type of the second parameter of `client.set` which is `Bytes`. So the `Bytes` type must implement the `Into` or `From` trait. Assuming it follows the advice in the docs for `Into`, it would just implement the `From` trait. So now I'm looking at the documentation for `std::str::Bytes` here ([Bytes in std::str - Rust](https://doc.rust-lang.org/std/str/struct.Bytes.html)) for evidence of implementing the `From` trait. I see `From<T>` listed under "Blanket Implementations", but clicking that doesn't give any indication that it is related to the `str` type. I just happened to notice that `Bytes` implements the `FromStr` trait. Does calling the `into` method on a `&str` use this? More than just looking for an answer to my immediate question, I'm trying to learn how I could have discovered the answer on my own by correctly interpreting the documentation.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [February 15, 2021, 12:51am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/4 "2021-02-15T00:51:49Z")

</div>

[`Client::set`](https://docs.rs/mini-redis/0.4.0/mini_redis/client/struct.Client.html#method.set) takes a [`bytes::Bytes`](https://docs.rs/bytes/1.0.1/bytes/struct.Bytes.html), not a `std::str::Bytes`.

If you click on the name `Bytes` in the docs, it will take you to the correct page. You don't need to search somewhere else for a type named `Bytes` and hope to find the right one.

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mvolkmann/32/18387_2.png) [@mvolkmann](https://users.rust-lang.org/u/mvolkmann)\
**Post date:** [February 15, 2021, 1:00am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/5 "2021-02-15T01:00:24Z")

</div>

I am using VS Code with rust-analyzer. When I hover over the call to `client.set` I see this:

 ![Screen Shot 2021-02-14 at 6.54.55 PM](https://us1.discourse-cdn.com/flex019/uploads/rust_lang/original/3X/f/e/fee6f15d41d39234cbbb05dd4493dbd148482d17.png)  
So I see the type "Bytes", but it doesn't tell me the namespace it comes from and clicking it doesn't do anything. How can I determine that it is really `bytes::Bytes`?

When I look at the documentation for `bytes::Bytes` I see this: `impl From<&'static str> for Bytes` which is the piece I was missing. This relies on me knowing that a call to the `into` method invokes this, but I guess that's just a part of Rust that beginners need to internalize.

---

<div class="post-metadata">

**Author:** ![alanhkarp](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alanhkarp/32/4011_2.png) [@alanhkarp](https://users.rust-lang.org/u/alanhkarp)\
**Post date:** [February 15, 2021, 1:06am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/6 "2021-02-15T01:06:14Z")

</div>

In VSCode, you can hit F12 while the cursor is on `Bytes` to be taken to the definition.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [February 15, 2021, 1:06am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/7 "2021-02-15T01:06:17Z")

</div>

Ah, it's unfortunate that types inside the documentation pop-up are not clickable. (They are in the online docs.)

In VS Code, if you right-click on `set` and choose “Go to Definition” then you can click/hover on the types of its parameters to get more information.

Rust Analyzer also has an “Open Docs Under Cursor” command that opens the HTML documentation in the web browser, but there is no default key binding for it.

---

<div class="post-metadata">

**Author:** ![EdmundsEcho](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/edmundsecho/32/18445_2.png) [@EdmundsEcho](https://users.rust-lang.org/u/EdmundsEcho)\
**Post date:** [February 15, 2021, 1:17am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/8 "2021-02-15T01:17:14Z")

</div>

In general, with certain trait methods, the type depends on what the context demands. `collect` is such an extreme case of this, you often can’t get away with using it without a type annotation.

One useful approach to learn what type is needed is to use an error throwing value such as `()` or `_`. For instance replace the `”hello”.into()` with `_`. The compiler will tell you what it was expecting. Of course the function signature is another source.

In general, see the compiler errors as less of an error generating machine, but more of a “here’s what to do next” tool. This is unique because of type inference and all the engineering that went into it.

Finally, I wouldn’t say it’s “bad practice” to call `into`, but more so to implement it directly instead of `From`. There are a few traits that are the reverse of the other; where one can be inferred from the other. It’s sometimes useful to track if applying each in sequence is the same as calling `id`.

It’s all really well laid out in the docs.

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mvolkmann/32/18387_2.png) [@mvolkmann](https://users.rust-lang.org/u/mvolkmann)\
**Post date:** [February 15, 2021, 1:26am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/9 "2021-02-15T01:26:58Z")

</div>

I tried your suggestion, replacing the line of code with this:

```rust
 client.set("hello", ()).await?;

```

The error message I get is "mismatched types; expected struct `bytes::bytes::Bytes`, found `()`". As far as I can tell `bytes::Bytes` exists, but `bytes::bytes::Bytes` does not. Is this an error in the error message?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [February 15, 2021, 1:45am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/10 "2021-02-15T01:45:53Z")

</div>

> [@mvolkmann](#):
>
> As far as I can tell `bytes::Bytes` exists, but `bytes::bytes::Bytes` does not.

Looking [at the source code](https://docs.rs/bytes/1.0.1/src/bytes/bytes.rs.html#94-100), the type is actually located at `bytes::bytes::Bytes`, where `bytes` is a private module of the `bytes` crate; the `Bytes` type then is re-exported [here](https://docs.rs/bytes/1.0.1/src/bytes/lib.rs.html#91) (with `pub use`). Still it feels kind-of undesirable for the compiler to print out the original path of a type (from an external crate) when that path is containing private modules and the type is available (and used) through some different, public-modules-only path. Especially when there’s only a single kind-of “canonical” such public path. On the other hand, note how complex it was to even _describe_ this undesirable setting; especially considering that it’s probably also desirable to always use _the same_ qualified path in error messages if it’s _the same type_, even if it was imported through different re-exports, so I guess improving the situation might be somewhat nontrivial.

* * *

_Edit:_ There seems to be a long-standing GitHub issue about this problem, too

> <https://github.com/rust-lang/rust/issues/21934>
>
> If a private type is reexported at some public location, the Rust compiler will …report the private location when referring to the type. It'd be more helpful if the compiler reported the public alias for it.
> 
> This was reported by Coda Hale on Twitter.
> 
> \----
> 
> Update by pnkfelix: here is a standalone bit of code illustrating the problem. (\[play\](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=ba6fa8cd0b3b03f756ebc8cbd95597a4)):
> 
> \`\`\`rust
> mod a {
> mod m { pub struct PrivateName; }
> pub use m::PrivateName as PublicName;
> }
> 
> fn main() {
> // a::m::PrivateName; // would error, "module \`m\` is private"
> a::PublicName;
> a::PublicName.clone();
> }
> \`\`\`
> 
> As of nightly 2019-06-17, this issues the diagnostic:
> 
> \`\`\`
> \> error\[E0599\]: no method named \`clone\` found for type \`a::m::PrivateName\` \[...\]
> ^^^^^^^^^^^^^^^^^
> |
> (author of \`fn main\` wants to see \`a::PublicName\` here)
> \`\`\`

---

<div class="post-metadata">

**Author:** ![H2CO3](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@H2CO3](https://users.rust-lang.org/u/H2CO3)\
**Post date:** [February 15, 2021, 6:13am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/11 "2021-02-15T06:13:01Z")

</div>

> [@EdmundsEcho](#):
>
> Finally, I wouldn’t say it’s “bad practice” to call `into` , but more so to implement it directly instead of `From` .

That's not something that should be done in general, and I'm not actually sure if it's ever the right thing to do. `From/Into` duality is an idiom that Rust users will have to learn. OTOH implementing `Into` directly makes it impossible to implement `From` due to the blanket impl, and it in turn prevents `From`-based generics from working.

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [February 15, 2021, 8:40am UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/12 "2021-02-15T08:40:27Z")

</div>

I do see how this makes the code less readable than it could otherwise be. I would be happy to accept a PR that changes that line to use `Bytes::from("world")` instead of `"world".into()`.

---

<div class="post-metadata">

**Author:** ![EdmundsEcho](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/edmundsecho/32/18445_2.png) [@EdmundsEcho](https://users.rust-lang.org/u/EdmundsEcho)\
**Post date:** [February 15, 2021, 1:52pm UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/13 "2021-02-15T13:52:38Z")

</div>

Ah. Great information.

I over-interpreted the following in the docs; the key miss was “prefer using... over `From`:

> Prefer using [`Into`](https://doc.rust-lang.org/std/convert/trait.Into.html) over [`From`](https://doc.rust-lang.org/std/convert/trait.From.html) when specifying trait bounds on a generic function to ensure that types that only implement [`Into`](https://doc.rust-lang.org/std/convert/trait.Into.html) can be used as well.

... the scope of the statement is “specifying trait bounds”. Not more generally based on what people are saying in this series of posts.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [May 16, 2021, 1:53pm UTC](https://users.rust-lang.org/t/after-3-months-still-learning-to-read-rust-code/55616/14 "2021-05-16T13:53:26Z")

</div>

Closed on May 16, 2021, 1:53pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
