是的,Rust 的 toml
庫支持多文檔。你可以使用 toml
庫來解析和生成包含多個文檔的 TOML 文件。每個文檔可以有自己的鍵值對,它們之間用分隔符(通常是雙引號)包圍。
以下是一個簡單的示例,展示了如何在 Rust 中使用 toml
庫處理多文檔的 TOML 文件:
use toml::Value;
fn main() {
let mut config = Value::from("[[database]]\nhost = \"localhost\"\nport = 5432\n\n[[api]]\nkey = \"1234567890abcdef\"");
// 解析第一個文檔
let database_config: Value = config.get("database").unwrap().clone();
println!("Database config: {:?}", database_config);
// 解析第二個文檔
let api_config: Value = config.get("api").unwrap().clone();
println!("API config: {:?}", api_config);
}
在這個示例中,我們創建了一個包含兩個文檔的 TOML 字符串。然后,我們使用 toml
庫的 Value
類型來解析這個字符串,并使用 get
方法分別獲取每個文檔的配置。
輸出結果如下:
Database config: Value({
"host": "localhost",
"port": 5432
})
API config: Value({
"key": "1234567890abcdef"
})
這個示例展示了如何在 Rust 中使用 toml
庫處理多文檔的 TOML 文件。你可以根據自己的需求對這個示例進行修改和擴展。