- Rust Quick Start Guide
- Daniel Arbuckle
- 329字
- 2021-06-10 19:46:06
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.
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.
- Android 9 Development Cookbook(Third Edition)
- Java 9 Programming Blueprints
- 從0到1:HTML+CSS快速上手
- Linux環境編程:從應用到內核
- Blender 3D Incredible Machines
- Python時間序列預測
- Learning Concurrency in Kotlin
- Building Serverless Web Applications
- Python Digital Forensics Cookbook
- PhoneGap 4 Mobile Application Development Cookbook
- C語言程序設計
- 現代CPU性能分析與優化
- 從零開始學UI設計·基礎篇
- Distributed Computing with Python
- HTML5 Game Development by Example:Beginner's Guide(Second Edition)