相关疑难解决方法(0)

为什么我需要导入特征以使用它为类型定义的方法?

我有一个非常简单的Rust代码示例,无法编译:

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);
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

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` …
Run Code Online (Sandbox Code Playgroud)

rust

17
推荐指数
1
解决办法
3686
查看次数

标签 统计

rust ×1