News for hackers. Stuff that matters.
Re:Announcing Tokio 0.1 by DangerousPie on Wednesday January 11, 2017 @01:03PM (#13375945)
On a related note, how does the `parse()` in `let addr = "127.0.0.1:12345".parse().unwrap();` work? I am assuming that this parses a IP:port string into its components, but it looks like this is just calling a method of the basic String class. So how does it infer what `parse()` is supposed to do?
Re:Announcing Tokio 0.1 by Coding_Cat on Wednesday January 11, 2017 @01:12PM (#13376033)
parse() is generic over it's return type (which is Result<F, F::Err> where F: FromStr) [1]. The value of which can be inferred.

The result of parse().unwrap() is of type F, which means addr is of type 'F'. afterwards when we call `let sock = TcpListener::bind(&addr, &handle).unwrap();` the compiler can infer from the signature of `TcpListener::bind`[2] what F has to be, namely `std::net::SocketAddr`

[1] https://doc.rust-lang.org/std/primitive.str.html [2] https://tokio-rs.github.io/tokio-core/tokio_core/net/struct....

Re:Announcing Tokio 0.1 by koolba on Wednesday January 11, 2017 @01:43PM (#13376338)
Where is string to address parse implementation defined? I took a peek at SocketAddr and see a from_str function. Does that name have special significance?
Re:Announcing Tokio 0.1 by ucarion on Wednesday January 11, 2017 @01:58PM (#13376468)
The method `from_str` is from the FromStr trait. [1] The `parse` function only works on types that implement FromStr. It's "special" in that it's the conventional way to do things, but the compiler does not treat FromStr specially.

[1]: https://doc.rust-lang.org/std/str/trait.FromStr.html

Re:Announcing Tokio 0.1 by steveklabnik on Wednesday January 11, 2017 @01:52PM (#13376421)
It's at the very bottom of https://doc.rust-lang.org/std/net/enum.SocketAddr.html, the part that says

  impl FromStr for SocketAddr
If you look at parse https://doc.rust-lang.org/std/primitive.str.html#method.pars... it says

    fn parse<F>(&self) -> Result<F, F::Err> where F: FromStr
So any type that implements FromStr can be parse'd from a &str.
Re:Announcing Tokio 0.1 by amichal on Wednesday January 11, 2017 @02:33PM (#13376761)
which has a link to the actual src which is here-ish and is what you might expect https://doc.rust-lang.org/src/std/up/src/libstd/net/parser.r...
Re:Announcing Tokio 0.1 by winstonewert on Wednesday January 11, 2017 @01:53PM (#13376428)
If you look closely, you'll see that the from_str function is part of the implementation of the FromStr trait for the SocketAddr struct. Its this FromStr trait that the parse() method depends on.
Re:Announcing Tokio 0.1 by carllerche on Wednesday January 11, 2017 @01:06PM (#13375969)
Rust uses type inference to figure that out. Because `addr` gets passed to `TcpListener::bind` which requires a `SocketAddr`, the type inferencer knows that `addr` should be a SocketAddr and so it all works :)
Re:Announcing Tokio 0.1 by curun1r on Wednesday January 11, 2017 @01:58PM (#13376471)
You might also add that it only works because SocketAddr implements FromStr. Type inference will only determine which version of from_str to call, it won't actually do the parsing.
Re:Announcing Tokio 0.1 by flukus on Wednesday January 11, 2017 @06:44PM (#13378548)
That type inference goes well beyond most others though.
Re:Announcing Tokio 0.1 by scott_karana on Wednesday January 11, 2017 @06:02PM (#13378273)
Wow, thank you for pointing that out!

Due to the resulting thread, I feel legitimately excited to sit down and learn a new-to-me language for the first time in ages :)

Re:Announcing Tokio 0.1 by leshow on Wednesday January 11, 2017 @01:16PM (#13376079)
I noticed that too, because it get's passed after as an argument, Rust is able to infer it's type.