mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-25 13:06:22 +00:00
135 lines
2 KiB
Markdown
135 lines
2 KiB
Markdown
|
---
|
||
|
id: 9062c9c0-b2d5-4719-b15b-6fe5122b6a9e
|
||
|
title: Rust Generics
|
||
|
---
|
||
|
|
||
|
# Function definitions
|
||
|
|
||
|
We read the following definition as: the function largest is generic
|
||
|
over some type T. This function has one parameter named list, which is a
|
||
|
slice of values of type T. The largest function will return a reference
|
||
|
to a value of the same type T.
|
||
|
|
||
|
``` rust
|
||
|
fn largest<T>(list: &[T]) -> &T {
|
||
|
let mut largest = list[0];
|
||
|
|
||
|
for item in list {
|
||
|
if item > largest {
|
||
|
largest = item;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
largest
|
||
|
}
|
||
|
```
|
||
|
|
||
|
# Struct definitions
|
||
|
|
||
|
``` rust
|
||
|
struct Point<T> {
|
||
|
x: T,
|
||
|
y: T,
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let integer = Point { x: 5, y: 10 };
|
||
|
let float = Point { x: 1.0, y: 4.0 };
|
||
|
}
|
||
|
```
|
||
|
|
||
|
A struct can also have multiple generic types:
|
||
|
|
||
|
``` rust
|
||
|
struct Point<T, U> {
|
||
|
x: T,
|
||
|
y: U,
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let both_integer = Point { x: 5, y: 10 };
|
||
|
let both_float = Point { x: 1.0, y: 4.0 };
|
||
|
let integer_and_float = Point { x: 5, y: 4.0 };
|
||
|
}
|
||
|
```
|
||
|
|
||
|
# Enum Definitions
|
||
|
|
||
|
``` rust
|
||
|
#![allow(unused)]
|
||
|
fn main() {
|
||
|
enum Option<T> {
|
||
|
Some(T),
|
||
|
None,
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And again with multiple generics:
|
||
|
|
||
|
``` rust
|
||
|
#![allow(unused)]
|
||
|
fn main() {
|
||
|
enum Result<T, E> {
|
||
|
Ok(T),
|
||
|
Err(E),
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
# Method Definitions
|
||
|
|
||
|
``` rust
|
||
|
struct Point<T> {
|
||
|
x: T,
|
||
|
y: T,
|
||
|
}
|
||
|
|
||
|
impl<T> Point<T> {
|
||
|
fn x(&self) -> &T {
|
||
|
&self.x
|
||
|
}
|
||
|
fn y(&self) -> &T {
|
||
|
&self.y
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let p = Point { x: 5, y: 10 };
|
||
|
|
||
|
println!("p.x = {} and p.y={}", p.x(), p.y());
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And again with multiple generics:
|
||
|
|
||
|
``` rust
|
||
|
struct Point<T, U> {
|
||
|
x: T,
|
||
|
y: U,
|
||
|
}
|
||
|
|
||
|
impl<T, U> Point<T, U> {
|
||
|
fn mixup<V, W>(self, other: Point<V, W>) -> Point<T, W> {
|
||
|
Point {
|
||
|
x: self.x,
|
||
|
y: other.y,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let p1 = Point { x: 5, y: 10.4 };
|
||
|
let p2 = Point { x: "Hello", y: 'c' };
|
||
|
|
||
|
let p3 = p1.mixup(p2);
|
||
|
|
||
|
println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
|
||
|
}
|
||
|
```
|
||
|
|
||
|
# Performance of Code Using Generics
|
||
|
|
||
|
The Rust compiler is very very clever and using generics has no
|
||
|
performance penalty
|