9

Rust/WinRT 预览版发布,微软在 Rust 大道上又进了一步

 4 years ago
source link: https://www.infoq.cn/article/BlIPrjObQHjRzm4ihTmo
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Rust/WinRT 是 Windows 运行时的一种语言映射,现在发布了预览版,可以通过自然惯用的方式来调用 Windows API,类似于 C++/WinRT 等其他语言映射。

借助 Rust/WinRT,你可以直接使用由 API 元数据生成的动态代码来调用 WinRT API,不管是过去的、现在的还是未来的,并可以直接调用 Rust 包,就好像调用另一个 Rust 模块一样。

微软首席软件工程师 Kenny Kerr 解释说,Rust 开发人员可以使用 Rust/WinRT 开发 Windows 应用程序和组件,以及 NT 服务或 Windows 驱动程序。下面演示了如何使用 XmlDocument 类来解析 XML 文档:

复制代码

usewindows::data::xml::dom::*;
letdoc = XmlDocument::new()?;
doc.load_xml("<html>hello world</html>")?;
letroot = doc.document_element()?;
assert!(root.node_name()? =="html");
assert!(root.inner_text()? =="hello world");

微软努力让 Rust/WinRT 代码尽可能看起来接近惯用的 Rust 代码,比如,蛇形命名到驼峰命名的自动转换、对 Rust null 合并操作符? 的支持、简化错误传递,等等。

根据 InfoQ 之前的报道,一段时间以来,微软一直在探索将 Rust 作为开发安全软件的解决方案。微软工程师 Ryan Levick 和 Sebastian Fernandez 在上一界巴塞罗那 RustFest 大会上解释说,微软期望 Rust 能够让高性能关键组件的开发变得更安全,尽管 Rust 社区仍然需要克服很多困难才能让 Rust 及其工具链为 Windows 提供一等支持。

这个新项目似乎证实了微软对待在开发中使用 Rust 这件事是认真的:

长期以来,微软一直将 C++ 作为开发的基础,但也面临着一些挑战,尤其是在安全方面。Rust 是一种有趣的编程语言,它在很多方面与 C++ 非常相似,在编译、运行时模型、类型系统和确定性终止机制方面都做得很好。它有自己独特的学习曲线,不过它可以解决困扰 C++ 项目的一些最令人头疼的问题,而且它的设计从一开始就是以内存安全和安全并发作为核心原则。

要在项目中使用 Rust/WinRT,可以在 Cargo.toml 文件添加依赖:

复制代码

[dependencies]
winrt = { git ="https://github.com/microsoft/winrt-rs"}

这样就可以导入任意 Windows 模块:

复制代码

usewinrt::*;
import!(
dependencies
os
modules
"windows.data.xml.dom"
"windows.foundation"
"windows.ui"
);

Rust/WinRT 现在只提供了早期预览版,虽然已经足够成熟了。Robert Mikhayelyan 的 Rust 扫雷游戏 提供了非常好的示例。

原文链接:

Rust/WinRT Brings Microsoft Closer to Adopting Rust Internally


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK