Rust或将添加关键字泛型:嫌Rust不够复杂?
2022-8-3 18:12:39 Author: mp.weixin.qq.com(查看原文) 阅读量:13 收藏

出品 | OSC开源社区(ID:oschina2013)
负责 Rust 语言特性的团队成员发起了 Keyword Generics Initiative,称此举旨在探索是否要为 Rust 添加关键字泛型

"Initiative" 是 Rust 治理模式中的术语,与 "working group" 和 "team" 不同,其具有临时性质。"Initiative" 存在的意义是为了探索、设计和实现特定的工作内容。一旦这项工作结束,临时组建的 "Initiative" 也就随之结束。

据介绍,虽然 Rust 支持泛型,但无法为其他常见的由关键字声明的事物添加泛型。例如使用 const 或 async 的函数。
不过 “关键字泛型” 不仅仅是为了异步而设计,其最终目的是让其适用于所有修饰符关键字 —— 包括可能将来才定义的关键字。总的来说,他们希望通过此次对 “关键字泛型” 的探索来填补这一空白:为 const 和 async 等关键字添加泛型的能力。
示例代码
async<A> trait Read {    async<A> fn read(&mut self, buf: &mut [u8]) -> Result<usize>;    async<A> fn read_to_string(&mut self, buf: &mut String) -> Result<usize> { ... }}
/// Read from a reader into a string.async<A> fn read_to_string(reader: &mut impl Read * A) -> std::io::Result<String> { let mut string = String::new(); reader.read_to_string(&mut string).await?; string}
目前尚未有关于 “关键字泛型” 的 RFC 文档,团队成员称现在处于 "pre-RFC" 阶段。此外他们还表示,对于这项特性,即便保证兼容性是最困难的工作之一,但依然会最大限度地向后兼容。

相关讨论:

Reddithttps://www.reddit.com/r/rust/comments/w9gi48/announcing_the_keyword_generics_initiative/

Lobsters:https://lobste.rs/s/hyd2sx/announcing_keyword_generics_initiative

相关链接:

https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-generics.html

往期推荐

我是 polarisxu,北大硕士毕业,曾在 360 等知名互联网公司工作,10多年技术研发与架构经验!2012 年接触 Go 语言并创建了 Go 语言中文网!著有《Go语言编程之旅》、开源图书《Go语言标准库》等。

坚持输出技术(包括 Go、Rust 等技术)、职场心得和创业感悟!欢迎关注「polarisxu」一起成长!也欢迎加我微信好友交流:gopherstudio


文章来源: http://mp.weixin.qq.com/s?__biz=MzAxNzY0NDE3NA==&mid=2247490242&idx=1&sn=855a92fd232bdf2e57d62134fee589ea&chksm=9be33523ac94bc358a2eb875a768ec3d932bbf6e67ee8adddcc25529e2bf5a19eae20f511307#rd
如有侵权请联系:admin#unsafe.sh