美文网首页程序员
几个类型转换Trait

几个类型转换Trait

作者: swapmem | 来源:发表于2017-03-31 23:33 被阅读146次

    Rust标准库std::convert模块下定义了几个trait用于类型之间的转换。本文将对它们做简单介绍。

    From & Into


    定义

    FromInto实现的是value-to-value的之间的转换。首先看看From在标准库中的定义:

    pub trait From<T>: Sized {
        fn from(T) -> Self;
    }
    

    这个From trait只有一个方法fn from(T) -> Self,它以类型T作为参数并返回Self。可以将这个方法签名写的更直观fn from(SomeType) -> ExpectedTypeSomeType是我们要进行转换的类型,ExpectedType是我们期待转换的目标类型。

    再来看看Into在标准库中的定义:

    pub trait Into<T>: Sized {
        fn into(self) -> T;
    }
    

    他与From的最大区别就是方法的参数和返回值对调了,由此可以得知它们的作用在语义上应该是相反的。方法签名可以这样来看待fn into(SelfType) -> TargetTypeSelfType是要进行转换的源类型,TargetType是转换之后的目标类型。

    使用场景

    通常情况下,我们只需要为类型实现From,相应的Into,标准库已经自动为我们实现了。From最常应用在自定义的错误类型处理中, Rust Book的Error Handling章节中Composing custom error types一节有From使用方法的很好示例。

    另外,某些情况下,在函数签名中使用Into, 可以使函数可以接受的参数类型更广, 使函数接口更加通用。下面是一个例子:

    fn accept_string(s: String)
    fn accept_str_and_string<T: Into<String>>(s: T) 
    

    我们期望有一个函数既能处理String类型的参数,又能处理&str类型的参数。在上面两个函数中,accept_string()只接受String类型的参数,假如我们有一个字面常量"hello"作为参数,那么我们需要像这样来调用:

    accept_string("hello".to_string())
    
    

    这看起来不是太优雅。如果使用accept_str_and_string(),那么就可以直接像这样调用:

    accept_str_and_string("hello")
    
    

    "hello"&str类型,而&str类型实现了Into<String>,在函数内部调用into()方法就可将&str类型转换为String。因此,accept_str_and_string()既可以接受"hello",也可以接受“hello".to_string(),这在某些情况下使得函数的通用性增加了不少,给函数调用者提供了很大的方便。

    AsRef & AsMut


    AsRefAsMut实现的是reference-to-reference之间的转换。因为他们是引用之间的转换,所以这种转换带来的开销相比上面提到的value-to-value之间的转换小很多。下面是他们在标准库中的定义:

    pub trait AsRef<T: ?Sized> {
        fn as_ref(&self) -> &T;
    }
    
    pub trait AsMut<T: ?Sized> {
        fn as_mut(&mut self) -> &mut T;
    }
    

    AsRefAsMut没有什么特殊的地方,他们主要的用途就是reference-to-reference之间轻量级的转换。

    结论

    总结起来他们的使用方法可以用下面的一张表来概括:

    Recive Return
    From T Self
    Into self T
    AsRef &self &T
    AsMut &mut self &mut T
    Reference
    1. https://ricardomartins.cc/2016/08/03/convenient_and_idiomatic_conversions_in_rust
    2. http://stackoverflow.com/questions/29812530/when-should-i-implement-stdconvertfrom-vs-stdconvertinto
    3. https://doc.rust-lang.org/book/error-handling.html#composing-custom-error-types
    4. https://llogiq.github.io/2015/07/30/traits.html

    相关文章

      网友评论

        本文标题:几个类型转换Trait

        本文链接:https://www.haomeiwen.com/subject/zcdqottx.html