是否可以为函数的泛型类型创建一个具有特征边界的类型别名?

编程入门 行业动态 更新时间:2024-10-27 22:28:21
本文介绍了是否可以为函数的泛型类型创建一个具有特征边界的类型别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

此代码:

pub type Foo<T: Read> = fn(bar: T);

产生 错误 E0122(在较新版本的Rust,这只是一个警告):

yields error E0122 (in newer versions of Rust, it is only a warning):

已尝试向类型别名添加通用约束.这个约束完全被忽略.为了向后兼容,Rust仍然允许此警告.考虑以下示例:

An attempt was made to add a generic constraint to a type alias. This constraint is entirely ignored. For backwards compatibility, Rust still allows this with a warning. Consider the example below:

trait Foo {}

type MyType<R: Foo> = (R, ());

fn main() {
    let t: MyType<u32>;
}

我们可以声明一个 MyType 类型的变量,尽管事实上u32 没有实现 Foo.因此,应避免使用与类型别名一致的通用约束.

We're able to declare a variable of type MyType<u32>, despite the fact that u32 does not implement Foo. As a result, one should avoid using generic constraints in concert with type aliases.

是否可以创建一个包含对函数指针的特征要求的类型别名?显然,编译器告诉我没有类型,但不知道是否还有其他我没有想到的函数选项.

Is it possible to create a type alias that contains trait requirements on a function pointer? Obviously the compiler is telling me no for types, but didn't know if there was another option for functions that I wasn't thinking of.

推荐答案

目前看来不可能,也没有解决方法.

At this time, it does not seem to be possible and no workarounds exist.

这篇关于是否可以为函数的泛型类型创建一个具有特征边界的类型别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 01:54:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198692.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   别名   边界   创建一个   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!