Hacker News new | past | comments | ask | show | jobs | submit login

I've been using Scala for years and have been eying Rust for a while. Nice to see lots of the things I like from Scala carry over. In particular this seems like a less magical version of Scala implicits, which seem incredibly cool at first until you realize that a particular library or framework implements implicits for everything, and tracking down the source for a given function involves guessing the signature or chasing down an implicit implicit conversion chain that gets you to one.

One thing I'm not sure about though, the article talks about implementing the Into trait, then quickly segues over to From. When would I use Into, when From, and do they both lead to the same end (I.e. a function that takes Into<Whatever>?) I've looked at the docs for each, and maybe I just haven't had enough coffee yet but the distinction isn't too clear.




> When would I use Into, when From

The standard library contains this code:

    // From implies Into
    #[stable(feature = "rust1", since = "1.0.0")]
    impl<T, U> Into<U> for T where U: From<T> {
        fn into(self) -> U {
            U::from(self)
        }
    }
So, you basically only ever implement From, and you get the equivalent Into for free.

More specifically, you would usually use `Into` to bound a generic function:

    fn is_hello<T: Into<Vec<u8>>>(s: T) {
        let bytes = b"hello".to_vec();
        assert_eq!(bytes, s.into());
    }
    
    let s = "hello".to_string();
    is_hello(s);
Whereas you'd use From to perform a conversion:

    let string = "hello".to_string();
    let other_string = String::from("hello");
    
    assert_eq!(string, other_string);
These APIs are pretty new; they landed _right_ before beta happened. We used to have FromFoo traits, and this generic system replaced them.


Maybe a bit off topic.

I've always heard that traits in rust are basically just type classes in haskell. But I'm not quite sure how you would implement this trait as a typeclass.

Specifically, from what I'm seeing here is that if you just implement From you've also now implemented Into. Is there an analog in haskell? I also can't figure out how to specify a type class that is parametrized the way into is.

Can anyone help by shedding some light on how you might go about this in haskell? Or where traits in rust differ from typeclasses in haskell?


Standard Haskell has only single parameter type classes. With MultiParamTypeClasses you can define

  class Into a b where
    into :: a -> b
Note that From would be the exact same class. I guess the distinction in Rust is relevant because of ownership.


Thank you! This is the best explanation I've been able to find for how to use From, instead of Into.

One thing that's useful about From is that you can implement From<SomePublicStruct> for YourPrivateStruct, but the compiler complains about Into<YourPrivateStruct> for SomePublicStruct.

SomePublicStruct could be (i32, i32) and YourPrivateStruct could be MyPoint, for example.

Making YourPrivateStruct public (with pub struct) fixes things, but that's not always what you want.


Any time. I myself checked in with IRC as I posted. :) Like I said, these traits are really new, and so they don't have great docs yet...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: