
Rust因其他用途而广受欢迎,但通常并不用于Web开发。然而,这种情况可能会改变。亚马逊云服务首席工程师卡尔·勒奇上周在ScyllaDB举办的2026年P99虚拟会议上表示,有了AI来承担繁重的工作,鉴于Rust的运行时性能和安全性保证,程序员们或许可以考虑使用这门编程语言。勒奇在会上展示了他的最新项目Topcoat,这是一个用于运行用Rust编写的Web应用程序的框架。
Popular for other uses, Rust isn't typically deployed for web development. That could change, however. With AI here to do the heavy lifting, coders may want to consider the programming language, given its runtime performance and safety guarantees, suggested Carl Lerche, an Amazon Web Services principal engineer, speaking at ScyllaDB’s 2026 P99 virtual conference last week. Lerche was there to demonstrate his latest project, Topcoat, a framework for running web applications written in Rust.
勒奇最为人熟知的或许是创建了Tokio,这是该生态系统中最为流行的异步Rust运行时。“三年前,如果(我)站出来说Rust可能是构建Web应用的最佳语言,你大概会说我疯了,”他对虚拟听众说道。Web开发者倾向于使用他们最熟悉的语言。如今,这意味着JavaScript,或者可能是Ruby。已经有多个Web框架在使用Rust。
Lerche is perhaps best known for creating Tokio, which is the most popular asynchronous Rust runtime in that ecosystem. “Three years ago, had [I] gone out and said that Rust could be the best language for building web apps, you probably would have called me crazy,” he told the virtual audience. Web developers gravitate towards the languages they find most familiar. These days, that means JavaScript, or possibly Ruby. A number of web frameworks already use Rust.
Leptos和Dioxus都将Rust编译为WebAssembly,大多数浏览器都能运行WebAssembly。这种方法非常适合繁重的计算任务,不过前期加载时间可能确实是个拖累。Topcoat则将你的Rust代码编译为对Web更友好的JavaScript。在勒奇看来,JavaScript仍然是与浏览器文档对象模型(DOM)交互的最佳方式。前端开发者肯·科尔曼在其YouTube频道“Code to the Moon”的一期总结视频中指出:“一般来说,任何Rust框架所采用的方法都可以追溯到另一个生态系统,通常是一个非常流行的生态系统。你很难指出某一个现有框架是(Topcoat的)主要灵感来源。”
Both Leptos and Dioxus compile Rust into WebAssembly, which most browsers run. This approach is great for heavy computational jobs, though the upfront loading time can be a real drag. Topcoat compiles your Rust code into more web-friendly JavaScript. For Lerche, JavaScript remains the best way to interact with the browser's Document Object Model (DOM). “Generally, the approach taken by any Rust framework can be traced back to another ecosystem, usually a very popular one. You can't really point to a single existing framework as [Topcoat’s] main source of inspiration,” noted frontend developer Ken Kohlman, in a summary video on his “Code to the Moon” YouTube channel.
在服务器端方面,勒奇承认Topcoat并不适合构建复杂应用——比如Figma那种规模的应用——但它可以轻松用于普通的、基于浏览器、由数据库驱动的应用,也就是当今90%的Web应用。
Walk on the server side Lerche admitted that Topcoat isn’t for building complex applications, on the scale of, say, Figma, but it can easily be used for the run-of-the-mill, browser-based, database-driven applications, which is to say 90 percent of the web today.
与当今大多数JavaScript框架不同,Topcoat并不认同当前对“响应式”的定义,即会话开始时将程序逻辑全部倾倒在浏览器端。相反,大部分逻辑在服务器上执行,只将结果发送给浏览器(开发者仍可使用Rust的一个子集编写客户端逻辑,Topcoat会自动将其编译为浏览器端的JavaScript)。这是一个旧理念,用更好的技术重新演绎。
Unlike most present-day JavaScript frameworks, Topcoat eschews the current definition of being reactive, where the program logic is dumped onto the browser when the session begins. Instead, most of the logic executes on the server, with only the results delivered to the browser (Developers can still write client-side logic using a subset of Rust, which Topcoat automatically compiles into JavaScript for the browser). It’s an old idea recast with better tech.
服务器端渲染早于响应式编程出现(参见Apache的服务器端包含)。Lerche承认,采用服务器端渲染,“听起来我们似乎要回到每次点击都整页刷新的旧时代,[但]你实际上几乎可以获得你所认为的响应式的全部体验。”但服务器端编程近来正在回潮,尤其是对于那些对React及其他框架不满的开发者而言。HTMX、Datastar和Phoenix LiveView都在服务器上渲染网页,将几乎完成的页面发送回浏览器。
Server-side rendering predates reactive programming (see Apache’s server-side includes). With server-side rendering, Lerche admitted, “It might sound like we're going back to the old days of full page refreshes for every click [but] you can actually get almost all of what you think of as reactive with server-side rendering.”But server-side programming has been making a comeback lately, especially for those devs disgruntled with React and other frameworks. HTMX, Datastar, and Phoenix LiveView all render web pages on the server, sending them back to the browser nearly completed.
Lerche说,Topcoat只是调整了它们的思路,使其与Rust良好契合。当用户猛击网页上的按钮时,整个页面不会刷新。服务器只返回并刷新该组件的HTML片段。前端JavaScript,后端Rust。Topcoat通过一次性渲染所有组件而非逐个渲染来节省加载时间。此外,数据加载不会阻塞页面加载。服务器可以返回带有占位符的网页,数据将流入这些占位符。
Topcoat is just tweaking their ideas to fit well with Rust, Lerche said. When a user smashes a button on a web page, the whole page isn’t refreshed. Just the HTML snippet for that component is returned and refreshed by the server. JavaScript in the front, Rust in the back Topcoat saves load times by rendering all the components at once, rather than one by one. Also, data loading does not block the loading of the page. The server can send back a web page with placeholders into which the data will stream.
Topcoat的一大秘方在于,它提供了一种方式,让已经在服务器上的组件直接与数据库通信,这得益于Lerche早先一个名为Toasty的项目——一个基于Rust的异步数据库对象关系映射器(他曾在P99 2024上谈到过)。
One big dollop of secret sauce that Topcoat has is that it provides a way for the component, which is already on the server, to speak directly to the database, thanks to an earlier project from Lerche called Toasty, a Rust-based asynchronous database object-relational mapper (which he spoke about at P99 2024).
他说:“你可以直接用数据完成渲染,不必再操心如何序列化、发送到客户端、再反序列化。”通过把数据加载深入到页面渲染的底层,服务器可以立即把HTML模板的头部发送给客户端,让浏览器在服务器渲染页面其余部分的同时开始预加载资源(如CSS或图片)。科尔曼说:“这种简洁性令人惊叹,而这正是我们在这一领域一直严重缺失的东西。我觉得一个并不真正懂Rust的人也能很快上手这个东西。”
“You can do your rendering right with the data immediately, so you don't have to figure out how to serialize it, send it to the client, deserialize it,” he said. By pushing data loading deep down into the page render, the server can send the head of the HTML template to the client immediately, allowing the browser to start preloading assets (like CSS or images) while the server renders the rest of the page. “The simplicity is incredible, and it is something that we have been sorely missing in this space. I feel like someone who doesn't really know Rust could get off the ground pretty quickly with this thing,” Kohlman said.
他确实同意莱尔谢的看法,即考虑到Topcoat只使用Rust语法的一个子集,它最适合较小的Web项目。他说:“能够随意使用完整的Rust语法,这一点是有其价值的。”®
He did agree with Lerche that Topcoat is best for smaller web projects, given that Topcoat only uses a subset of the Rust syntax. “There's something to be said for having the full Rust syntax at your disposal,” he said. ®