官术网_书友最值得收藏!

Type inference

As we noticed earlier, we can specify the type of a variable, but we often don't have to. That's because Rust has a feature called type inference, which often lets it figure out what type a variable is by looking at what we do with it. For example, if we were using the Tokio networking library, we might use code such as this:

let addr = "127.0.0.1:12345".parse()?;
let tcp = TcpListener::bind(&addr)?;

We didn't specify what type the addr variable should have. Even more interesting, we didn't tell the text address what kind of information we needed it to parse into.

Parsing means transforming a representation into usable data, approximately. Lots of things can be represented as a string of text, if you know how to parse that text into the information you really want.
The question marks in this example are part of Rust's error handling mechanism, and the ampersand is an operator that affects how the addr variable is shared with the function. We'll see more on both of those soon.

However, Rust can see that we're passing the addr variable (or rather, a reference to it, but more on that in the next chapter) as a parameter of the TcpListener::bind function, and it knows that that function needs a reference to a SocketAddr, so addr must be a SocketAddr. Then, since it has figured out that addr is a SocketAddr, it takes it a step further and determines that it should use the string parsing function that produces a SocketAddr as its resulting value.

Type inference can save an amazing amount of time in a language as strict as Rust. On the other hand, it can be surprising if you see an error message about a data type you've never heard of, because Rust decided that it was the one you needed. If that happens, try assigning the type you actually expect to your variable and see what the Rust compiler has to say afterward.

主站蜘蛛池模板: 乐亭县| 云浮市| 滦南县| 平潭县| 海阳市| 石城县| 留坝县| 石城县| 得荣县| 乐平市| 女性| 南宁市| 隆尧县| 霍山县| 金塔县| 清水河县| 南召县| 双柏县| 怀远县| 嵩明县| 遵化市| 滦南县| 陇南市| 宣汉县| 竹北市| 新宾| 体育| 三都| 彭山县| 鹤山市| 玛多县| 磐安县| 绥芬河市| 金坛市| 铁岭市| 文安县| 化德县| 贞丰县| 广元市| 乐安县| 石嘴山市|