I have a very simple Rust code example that does not compile:
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); }
Compiler Error:
error[E0599]: no method named `to_base64` found for type `&[u8]` in the current scope --> 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;`
It works if I import the trait explicitly:
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); }
Why do I need to use rustc_serialize::base64::ToBase64; ?
source share