Skip to content

函数和泛型

函数定义

rust
fn greet() {
    println!("Hello, rust!");
}
fn main() {
    greet();
}
rust
fn greet(name: &str) {
    println!("Hello, {}!", name);
}
fn main() {
    let name = "rust表情符🐶";
    greet(name);
}
rust
fn add(a: i32, b: i32) -> i32 {
    return a + b;
}
fn main() {
    println!("{}", add(1, 2));
}
rust
trait Add {
    type Output;
    fn add(self, rhs: Self) -> Self::Output;
}
impl Add for i32 {
    type Output = i32;
    fn add(self, rhs: Self) -> Self::Output {
        return self + rhs;
    }
}
impl Add for f64 {
    type Output = f64;
    fn add(self, rhs: Self) -> Self::Output {
        return self + rhs;
    }
}
fn main() {
    println!("{}", 2.1.add(1.7));
    println!("{}", 1.add(2));
}
rust
fn add(a: i32, b: i32) -> i32 {
    return a + b;
}
fn subtract(a: i32, b: i32) -> i32 {
    return a - b;
}
fn calc(a: i32, b: i32, f: fn(i32, i32) -> i32) -> i32 {
    return f(a, b);
}
fn main() {
    println!("{}", calc(1, 2, add));
    println!("{}", calc(1, 2, subtract));
}

泛型

基础使用

rust
fn function_name<T>(param: T) -> T {
    return param;
}
// <T>: 指定函数的泛型类型
// (param: T): param是类型T

// 多个不同类型的参数时
fn function_name<T, U>(param1: T, param2: U) -> (T, U) {}
rust
// 查找最大值int
fn find_max_int(list: &[i32]) -> i32 {
    let mut max = list[0];
    for &item in list.iter() {
        if item > max {
            max = item;
        }
    }
    return max;
}

// 查找最大值char
fn find_max_char(list: &[char]) -> char {
    let mut max = list[0];
    for &item in list.iter() {
        if item > max {
            max = item;
        }
    }
    return max;
}

fn main() {
    let int_list = [1, 2, 3, 4, 5];
    println!("{}", find_max_int(&int_list));
    let char_list = ['a', 'b', 'c', 'd', 'e'];
    println!("{}", find_max_char(&char_list));
}
rust
fn find_max<T: PartialOrd + Copy>(list: &[T]) -> T {
    let mut max = list[0];
    for &item in list.iter() {
        if item > max {
            max = item;
        }
    }
    return max;
}

fn main() {
    let int_list = [1, 2, 3, 4, 5];
    println!("{}", find_max(&int_list));
    let char_list = ['a', 'b', 'c', 'd', 'e'];
    println!("{}", find_max(&char_list));
}

实际应用

通过泛型实现计算矩形面积

rust
fn area<T> + Copy>(x: T) -> T {
    return x * x;
}
fn main() {
    println!("{}", area(5));
    println!("{}", area(5.0));
    println!("{}", area(5.0f32));
    println!("{}", area(5.0f64));
}
// 提示异常
// help: consider restricting type parameter `T`
// 1 | fn area<T: std::ops::Mul<Output = T>>(x: T) -> T {
rust
fn area<T: std::ops::Mul<Output = T> + Copy>(x: T) -> T {
    return x * x;
}
fn main() {
    println!("{}", area(5));
    println!("{}", area(5.0));
    println!("{}", area(5.0f32));
    println!("{}", area(5.0f64));
}
rust
fn area<T>(x: T) -> T
where T: std::ops::Mul<Output = T> + Copy 
{
    return x * x;
}
fn main() {
    println!("{}", area(5));
    println!("{}", area(5.0));
    println!("{}", area(5.0f32));
    println!("{}", area(5.0f64));
}
rust
struct Rect<T> {
    width: T,
    height: T,
}

impl<T> Rect<T> {
    fn area(&self) -> T
    // 泛型约束
    where T: std::ops::Mul<Output=T> + Copy,
    {
        return self.width * self.height;
    }
}

fn main() {
    // 整形
    let r1 = Rect { width: 5, height: 10 };
    println!("{}", r1.area());

    // 浮点型
    let r2 = Rect { width: 5.0, height: 10.0 };
    println!("{}", r2.area());
}

人生感悟