一、Generic structures泛型结构体
泛型是一种编程语言的特性,它允许在代码中使用参数化类型,以便在不同地方使用
相同的代码逻辑处理多种数据类型,而无需为每种类型编写单独的代码!
作用:
1.提高代码的重用性
2.提高代码的可读性
3.提高代码的抽象度
1.泛型的应用类型
1.泛型定义结构体\枚举
2.泛型定义函数
3.泛型与特质
#[derive(Debug)]
struct Point<T> {
x: T,
y: T,
}
#[derive(Debug)]
struct PointTwo<T, E> {
x: T,
y: E,
}
fn main() {
let c1 = Point { x: 1.0, y: 2.0 };
let c2 = Point { x: 'x', y: 'y' };
println!("c1 {:?} c2 {:?}", c1, c2);
let c = PointTwo { x: 1.0, y: 'z' };
println!("{:?}", c);
// Zero-Cost Abstraction
}
编译及运行:
cargo run
Compiling ch1_generic_struct v0.1.0 (/home/wangji/installer/rust/project/ch23_generic_struct)
warning: fields `x` and `y` are never read
--> src/main.rs:3:5
|
2 | struct Point<T> {
| ----- fields in this struct
3 | x: T,
| ^
4 | y: T,
| ^
|
= note: `Point` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: fields `x` and `y` are never read
--> src/main.rs:9:5
|
8 | struct PointTwo<T, E> {
| -------- fields in this struct
9 | x: T,
| ^
10 | y: E,
| ^
|
= note: `PointTwo` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
warning: `ch1_generic_struct` (bin "ch1_generic_struct") generated 2 warnings
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
Running `target/debug/ch1_generic_struct`
c1 Point { x: 1.0, y: 2.0 } c2 Point { x: 'x', y: 'y' }
PointTwo { x: 1.0, y: 'z' }
二、Generic Function泛型函数
在Rust中,泛型也可以用于函数,使得函数能够处理多种类型的参数,提高代码的重用性和灵活性
1.泛型与普通函数
2.泛型与结构体中的方法
// 交换
fn swap<T>(a: T, b: T) -> (T, T) {
(b, a)
}
struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> {
// 构造函数
fn new(x: T, y: T) -> Self {
Point { x, y }
}
// 方法
fn get_coordinates(&self) -> (&T, &T) {
(&self.x, &self.y)
}
}
fn main() {
let result = swap::<f64>(0.1, 1.0);
let result: (f64, f64) = swap(0.1, 1.0);
println!("{:?}", result);
let str2 = swap("hh", "tt");
println!("str2.0 {} str2.1 {}", str2.0, str2.1);
let str2 = swap(str2.0, str2.1);
println!("str2.0 {} str2.1 {}", str2.0, str2.1);
let i32_point = Point::new(2, 3);
let f64_point = Point::new(2.0, 3.0);
let (x1, y1) = i32_point.get_coordinates();
let (x2, y2) = f64_point.get_coordinates();
println!("i32 point: x= {} y= {}", x1, y1);
println!("f64 point: x= {} y= {}", x2, y2);
// String 不要用&str
//最好使用:let string_point = Point::new("xxx".to_owned(), "yyyy".to_owned());
let string_point = Point::new("xxx", "yyyy");
println!("string point x = {} y = {}", string_point.x, string_point.y);
}
编译及运行
cargo run
Compiling ch24_func v0.1.0 (/home/wangji/installer/rust/project/ch24_func)
warning: unused variable: `result`
--> src/main.rs:23:9
|
23 | let result = swap::<f64>(0.1, 1.0);
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_result`
|
= note: `#[warn(unused_variables)]` on by default
warning: `ch24_func` (bin "ch24_func") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.33s
Running `target/debug/ch24_func`
(1.0, 0.1)
str2.0 tt str2.1 hh
str2.0 hh str2.1 tt
i32 point: x= 2 y= 3
f64 point: x= 2 y= 3
string point x = xxx y = yyyy
泛型函数
fn largest<T:PartialOrd+Copy>(list: &[T]) -> T {
let mut largest = list[0];
for &item in list {
if item > largest {
largest = item;
}
}
largest
}
fn main() {
let number_list = vec![34, 50, 25, 100, 65];
let result = largest(&number_list);
println!("The largest number is {}", result);
assert_eq!(result, 100);
let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];
let result = largest(&number_list);
println!("The largest number is {}", result);
assert_eq!(result, 6000);
}
结构体泛型
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 };
}
添加方法
struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> {
// 适用于任何类型的Point
fn x(&self) -> &T {
&self.x
}
}
impl Point<f64> {
// 适用于&f64的Point
fn y(&self) -> &f64 {
&self.x
}
}
fn main() {
let p = Point { x: 5, y: 10 };
println!("p.x = {}", p.x());
let p = Point { x: 5., y: 10.10 };//x()和y()都可以调用
}
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);
}
enum泛型
#![allow(unused)]
fn main() {
enum Option<T> {
Some(T),
None,
}
}
#![allow(unused)]
fn main() {
enum Result<T, E> {
Ok(T),
Err(E),
}
}
泛型函数的性能
enum Option<T>
{
Some(T)
None
}
#![allow(unused)]
fn main() {
let integer = Some(5);
let float = Some(5.0);
}
当 Rust 编译这些代码的时候,它会进行单态化。编译器会读取传递给 Option 的值并发现有两种 Option:一个对应 i32 另一个对应 f64。为此,它会将泛型定义 Option 展开为 Option_i32 和 Option_f64,接着将泛型定义替换为这两个具体的定义。
编译器生成的单态化版本的代码看起来像这样,并包含将泛型 Option 替换为编译器创建的具体定义后的用例代码:
enum Option_i32 {
Some(i32),
None,
}
enum Option_f64 {
Some(f64),
None,
}
fn main() {
let integer = Option_i32::Some(5);
let float = Option_f64::Some(5.0);
}