title: Why do I need to import a trait to use the methods it defines for a type?
body: <p>I have a very simple example of Rust code which fails to compile:</p>
  
  <pre><code>extern crate rustc_serialize;
  use rustc_serialize::base64;
  
  fn main() {
      let auth = format!("{}:{}", "user", "password");
      let auth_b64 = auth.as_bytes().to_base64(base64::MIME);
      println!("Authorization string: {}", auth_b64);
  }
  </code></pre>
  
  <p>The compiler error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0599]: no method named `to_base64` found for type `&amp;[u8]` in the current scope
   --&gt; src/main.rs:6:36
    |
  6 |     let auth_b64 = auth.as_bytes().to_base64(base64::MIME);
    |                                    ^^^^^^^^^
    |
    = help: items from traits can only be used if the trait is in scope
    = note: the following trait is implemented but not in scope, perhaps add a `use` for it:
            candidate #1: `use rustc_serialize::base64::ToBase64;`
  </code></pre>
  
  <p>It works if I import the trait explicitly:</p>
  
  <pre><code>extern crate rustc_serialize;
  
  use rustc_serialize::base64::{self, ToBase64};
  
  fn main() {
      let auth = format!("{}:{}", "user", "password");
      let auth_b64 = auth.as_bytes().to_base64(base64::MIME);
      println!("Authorization string: {}", auth_b64);
  }
  </code></pre>
  
  <p>Why do I need to <code>use rustc_serialize::base64::ToBase64;</code>?</p>