- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 122字
- 2021-07-02 13:35:29
Generic types
Generic structs: We can declare tuple structs and normal structs generically like so:
// generic_struct.rs
struct GenericStruct<T>(T);
struct Container<T> {
item: T
}
fn main() {
// stuff
}
Generic structs contain the generic type parameter after the name of the struct, as shown in the preceding code. With this, whenever we denote this struct anywhere in our code, we also need to type the <T> part together with the type.
Generic enums: Similarly, we can create generic enums as well:
// generic_enum.rs
enum Transmission<T> {
Signal(T),
NoSignal
}
fn main() {
// stuff
}
Our Transmission enum has a variant called Signal, which holds a generic value, and a variant called NoSignal, which is a no value variant.
推薦閱讀
- Mastering JavaScript Functional Programming
- ASP.NET Core 5.0開發(fā)入門與實戰(zhàn)
- 機械工程師Python編程:入門、實戰(zhàn)與進階
- Java設(shè)計模式及實踐
- Visual Basic程序設(shè)計與應(yīng)用實踐教程
- Hands-On Enterprise Automation with Python.
- QGIS:Becoming a GIS Power User
- Mastering Google App Engine
- TradeStation交易應(yīng)用實踐:量化方法構(gòu)建贏家策略(原書第2版)
- PHP 7+MySQL 8動態(tài)網(wǎng)站開發(fā)從入門到精通(視頻教學(xué)版)
- 從Java到Web程序設(shè)計教程
- 從零開始學(xué)C#
- OpenCV 3 Blueprints
- 大學(xué)計算機應(yīng)用基礎(chǔ)(Windows 7+Office 2010)(IC3)
- DB2SQL性能調(diào)優(yōu)秘笈