库模块
Slint 的库模块让一个 Rust crate 发布一组 .slint 组件,并让另一个 crate 通过 Cargo 的依赖系统导入它们。
使用方编写 import { Foo } from "@libname";,Cargo 的构建元数据会告诉 Slint 编译器在哪里找到库源。
注意:库模块是实验性的。 发布方需要
slint-build的experimental-module-buildsfeature,使用方需要 Slint 编译器的experimental-library-modulefeature。 跟踪 issue:slint-ui/slint#154。
提供库的 crate 在其 build.rs 中编译它的 .slint 入口点,并将其标记为库。
这会使 Cargo 发出 DEP_* 环境变量,供下游 crate 读取。
// build.rs of the publishing cratefn main() { let config = slint_build::CompilerConfiguration::new() .as_library("mywidgets"); slint_build::compile_with_config("ui/lib.slint", config).unwrap();}该 crate 的 Cargo.toml 必须声明 links = "mywidgets",以便 Cargo 将元数据转发给依赖它的 crate。
[package]name = "mywidgets"links = "mywidgets"依赖发布 crate 的 crate 通过 @ 前缀后跟库名来导入其组件:
import { FancyButton } from "@mywidgets";
export component App inherits Window { FancyButton { text: "Click me"; }}使用方无需额外的 build.rs 配置:Slint 编译器会通过 DEP_MYWIDGETS_SLINT_LIBRARY_* 环境变量发现该库,而 Cargo 会为 links 依赖自动转发这些变量。
当发布 crate 使用 as_library("name") 时,其构建脚本会发出以下元数据,Cargo 会将其以 DEP_NAME_* 的形式暴露给依赖方:
| Variable | Meaning |
|---|---|
SLINT_LIBRARY_NAME |
在 @name 导入中使用的库名。 |
SLINT_LIBRARY_SOURCE |
库入口 .slint 文件的绝对路径。 |
SLINT_LIBRARY_PACKAGE |
发布 crate 的包名。 |
SLINT_LIBRARY_MODULE |
生成代码所在的 Rust 模块路径(可选)。 |
无需跨 crate 传递的临时本地库,仍然可以直接用 slint_build::CompilerConfiguration::with_library_paths 注册。
该 API 是稳定的,不需要实验性 feature。
© 2026 SixtyFPS GmbH