简体   繁体   English

我应该在哪里保存我的配置文件 Rust

[英]Where should I save my config files in Rust

I need to persist information, such as the user's configuration for my program and their input.我需要保留信息,例如用户对我的程序的配置及其输入。 Where would the best place be to save this files on Linux and Windows?将这些文件保存在 Linux 和 Windows 上的最佳位置是哪里?

This is for a rust-cargo program, which will be installed into ~/.cargo/bin for its usage.这是一个rust-cargo程序,它将被安装到~/.cargo/bin以供使用。 I have tried to save my files into ~/.cargo but I don't know if this is appropiate, as I am new into Rust .我试图将我的文件保存到~/.cargo但我不知道这是否合适,因为我是Rust的新手。

There's nothing special for applications written in Rust.用 Rust 编写的应用程序没有什么特别之处。 Contrary to other solutions coming with a runtime, Rust builds normal applications relying on the standard env and practices.与运行时附带的其他解决方案相反,Rust 构建依赖于标准环境和实践的正常应用程序。 The normal rules of the system apply for configuration locations.系统的正常规则适用于配置位置。

On linux you should first query the XDG paths then use $HOME as fallback when it's not available.在 linux 上,您应该首先查询XDG路径,然后在$HOME不可用时使用它作为后备。

Here's how you can do it:以下是您的操作方法:

use std::env::var;

fn main() {
    let config_home = var("XDG_CONFIG_HOME")
        .or_else(|_| var("HOME").map(|home|format!("{}/.config", home)));
    println!("{:?}", config_home);
}

Note that several libraries do this job for you and take care of supporting alternate operating systems.请注意,有几个库会为您完成这项工作,并负责支持备用操作系统。

I won't link to them because they are many of them and they often change but your favourite search engine will direct you to the most popular ones if you search for "rust configuration directory".我不会链接到它们,因为它们很多,而且它们经常更改,但是如果您搜索“rust 配置目录”,您最喜欢的搜索引擎会将您定向到最受欢迎的搜索引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM