简体   繁体   English

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

[英]Why do I need to import a trait to use the methods it defines for a type?

I have a very simple example of Rust code which fails to compile:我有一个非常简单的 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);
}

The 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;为什么我需要use rustc_serialize::base64::ToBase64; ? ?

That's simply the way it is.就是这样。 In Rust a trait must be in scope for you to be able to call its methods.在 Rust 中,特征必须在范围内才能调用其方法。

As for why , the possibility of collisions is the reason why.至于为什么,碰撞的可能性是原因。 All the formatting traits in std::fmt ( Display , Debug , LowerHex , &c.) have the same method signature for fmt . std::fmt所有格式特征( DisplayDebugLowerHex等)都具有相同的fmt方法签名。 For example;例如; what would object.fmt(&mut writer, &mut formatter) do, for example?例如, object.fmt(&mut writer, &mut formatter)做什么? Rust's answer is “you must explicitly indicate by having the trait in scope where the method is.” Rust 的回答是“您必须通过在方法所在的范围内使用 trait 来明确指示。”

Note also how the error message says that “no method named `m` found for type `T` in the current scope ”.另请注意错误消息如何说明“在当前范围内找不到类型‘T’的名为‘m’的方法”。

Note that you don't have to import it if you want to use the trait method as a function instead of a method:请注意,您不必导入它,如果你想使用的特征方法的功能,而不是一个方法:

extern crate rustc_serialize;

use rustc_serialize::base64;

fn main() {
    let auth = format!("{}:{}", "user", "password");
    let auth_b64 = rustc_serialize::base64::ToBase64::to_base64(auth.as_bytes(), base64::MIME);
    //             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    println!("Authorization string: {}", auth_b64);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么需要定义特征来定义外部类型的实现? - Why do I need to define a trait to define an implementation on an external type? 我是否必须“使用”特征才能调用该特征中定义的方法? - Do I have to 'use' a trait in order to call methods defined in that trait? 为什么使用重叠方法的特征会改变类型推断结果? - Why `use`-ing a trait with overlapping methods changes type inference result? 如何在 Rust 中导入`str::to_string`? (一般情况下是否可以使用特征方法来做到这一点?) - How can I import `str::to_string` in Rust? (And is it possible to do this with trait methods in general?) 为什么我要在特征上实现方法而不是作为特征的一部分? - Why would I implement methods on a trait instead of as part of the trait? 为什么这里需要类型注解? - Why do I need the type annotation here? 如何反序列化为特征,而不是具体类型? - How do I deserialize into trait, not a concrete type? 在为闭包特征(Fn)创建整体实现时,为什么会得到“类型参数不受限制”的信息? - Why do I get “the type parameter is not constrained” when creating a blanket implementation for a closure trait (Fn)? 为什么我不必为类型实现Any特性,即使它是必需的? - Why do I not have to implement the Any trait for a type even though it is a required? 为什么即使它实现了 IntoIterator,对于泛型类型的引用,我也会收到错误“特征 `Iterator` 未实现”? - Why do I get the error “the trait `Iterator` is not implemented” for a reference to a generic type even though it implements IntoIterator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM