微软 Rust/WinRT 预览版发布
微软的 rust/winrt 项目已经在 github 开源:https://github.com/microsoft/winrt-rs。
Rust/WinRT 遵循 C++/WinRT 建立的传统,即使用标准语言和编译器为 Windows 运行时构建语言投影,从而为 Rust 开发人员调用 Windows API 提供了舒服的惯用方式。更多详情请看原文:https://blogs.windows.com/windowsdeveloper/2020/04/30/rust-winrt-public-preview/

这是一个短篇幅系列博文,分为两部分,在第一部分中,你将学习如何:在 Rust 中创建快速的 REST API 和将其连接到 PostgreSQL 数据库。
而在第二部分中,我们将比较我们的应用程序和 Go 应用程序的性能。感兴趣的朋友请看原文教程:https://docs.qovery.com/guides/tutorial/create-a-blazingly-fast-api-in-rust-part-1/
Rust 的 Type-level 编程Typestate 是在编程语言的类型系统中对状态机进行编码的概念。尽管不是特定于 Rust,但在 Rust 编程的中也有很多地方探讨了 typestate。
Typestate可以归结为四个想法:
每个状态都表示为唯一类型。状态转换仅可用作相应状态类型的方法。进行状态转换将返回新状态类型的状态机。状态转换会使旧状态无效。如下,这是一个 send-then-receive channel 状态机:
// Each state is a unique typestruct Receiving;struct Sending;// The state machine is parameterized by the statestruct Channel{ chan: ..., _state: PhantomData }// Methods for the state are uniquely associated with only the stateimpl Channel { // recv consumes ownership, ensuring old state is invalidated fn recv(mut self) -> (Channel , String) { let msg = self.chan.recv(); // The state type changes after executing a transition (unsafe { transmute(self) }, msg) }}impl Channel { fn send(mut self, msg: String) -> Channel { self.chan.send(msg); unsafe { transmute(self) } }}#[test]fn channel_test() { let c: Channel = Channel::new(); let c: Channel = c.send("hi"); let (c, msg) = c.recv(); // and so on}
这种模式对于简单的有限状态机有效,其中确定下一个状态的逻辑很简单。本篇博文中,我将探讨确定下一个状态不是那么简单的情况。在此过程中,我们将讨论类型级编程,或者如何使用 Rust 的类型系统对类型的计算进行编码。
本篇博文中的部分目标是在实践中显示类型级编程的价值。这些相同的机制已经用于更深奥的目的,例如表明 Rust 的类型系统已经实现 Turing,但我认为类型级编程确实可以帮助我们设计更好的系统!
更多请查看原文:http://willcrichton.net/notes/type-level-programming/
From 日报小组 @Jancd










