rust - 我怎样才能创建一个接受任何类型的函数?

标签 rust

我如何在 Rust 中创建一个接受任何类型或多个类型作为函数参数的函数?

我的第一种方法:

fn multiple_types(argument : _) {
    println!("{}",argument);
}

我尝试将类型占位符“_”作为参数,但这是不允许的...

最佳答案

来自 the documentation :

We can write functions that take generic types with a similar syntax:

fn takes_anything<T>(x: T) {
    // do something with x
}

关于rust - 我怎样才能创建一个接受任何类型的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30470172/

相关文章:

rust - 为什么我不能在 QEMU 中启动自定义内核?

plugins - 如何从 ConstVal 中取出一个元组?

type-conversion - 如何将不可变 View (切片?)作为八位字节借用给向量?

multithreading - 如何在 Rust 中进行 "fire and forget"调用?

rust - Cargo 构建结果为 "No match for id"

rust - 如何在 Rust 中连接两个切片?

rust - 如何制作具有 RAII 属性的订阅者对象?

rust - 函数中的可变生命周期 qs

module - 在多个文件中使用模块时无法编译项目 : "imports can only refer to extern crate names passed with --extern"

rust - 如何使用 Rust 枚举定义类型层次结构?