- Mastering Rust
- Rahul Sharma Vesa Kaihlavirta
- 356字
- 2021-07-02 13:35:29
Generic functions
To create a generic function, we place the generic type parameter immediately after the function name and before the parenthesis, like so:
// generic_function.rs
fn give_me<T>(value: T) {
let _ = value;
}
fn main() {
let a = "generics";
let b = 1024;
give_me(a);
give_me(b);
}
In the preceding code, give_me is a generic function with <T> after its name, and the value parameter is of type T. In main, we can call this function with any argument. During compilation, our compiled object file will contain two specialized copies of this function. We can confirm this in our generated binary object file by using the nm command, like so:

nm is a utility from the GNU binutils package for viewing symbols from compiled object files. By passing nm our binary, we pipe and grep for the prefix of our give_me function. As you can see, we have two copies of the function with random IDs appended to them to distinguish them. One of them takes a &str and the other a i32, because of two invocations with different arguments.
Generic functions are a cheap way to give the illusion of polymorphic code. I say illusion because after compilation, it is all duplicated code with concrete types as parameters. They come with a downside though, which is an increase in the size of the compiled object file due to code duplication. This is proportional to the number of concrete types that are used. In later sections, when we get to traits, we'll see the true form of polymorphism, trait objects. Still, polymorphism through generics is preferred in most cases because it has no runtime overhead, as is the case with trait objects. Trait objects should only be used when generics don't cater to the solution and cases where you need to store a bunch of types together in a collection. We'll see those examples when we get to trait objects. Next, we'll look at how we can make our structs and enums generic. We'll only explore how to declare them first. Creating and using these types are covered in the later sections.
- Go Web編程
- 程序員面試筆試寶典
- Dependency Injection in .NET Core 2.0
- HTML5+CSS3網(wǎng)站設(shè)計教程
- Visual Basic程序設(shè)計實驗指導(dǎo)(第4版)
- PostgreSQL Replication(Second Edition)
- Functional Kotlin
- Python機器學(xué)習(xí)基礎(chǔ)教程
- Learning R for Geospatial Analysis
- 零基礎(chǔ)學(xué)C語言程序設(shè)計
- Android嵌入式系統(tǒng)程序開發(fā):基于Cortex-A8(第2版)
- Node.js 12實戰(zhàn)
- Software-Defined Networking with OpenFlow(Second Edition)
- 編程的原則:改善代碼質(zhì)量的101個方法
- Spring Boot從入門到實戰(zhàn)