目标:
- 检测OS类型和基础信息
- File 和 IO 模块
- /etc或者注册表
OS related
rust
use std::env;
fn main() {
println!("OS module is not for check OS!\r\n");
println!("Use ENV to detect OS info:");
println!("OS: {}", env::consts::OS);
println!("ARCH: {}", env::consts::ARCH);
println!("FAMILY: {}", env::consts::FAMILY);
println!("DLL_EXTENSION: {}", env::consts::DLL_EXTENSION);
println!("EXE_EXTENSION: {}", env::consts::EXE_EXTENSION);
if cfg!(windows) {
println!("windows os!");
}else if cfg!(unix) {
println!("unix os!")
}else {
println!("unknown os!")
}
println!("If need more info, need third part package: sysinfo");
}
io, file
rust
use std::fs::File;
use std::io::{BufWriter, Read, Seek, SeekFrom, Write};
fn main() {
{
println!("Read Cargo.toml content!\r\n");
let mut f = File::open("Cargo.toml").unwrap();
let mut buf = [0u8; 1024];
_ = f.seek(SeekFrom::Start(0));
let n = f.read(&mut buf).unwrap();
println!(
"read {} byte, content: {}",
n,
String::from_utf8_lossy(&buf[..n])
);
_ = f.flush(); // 不用关闭,因为f超出作用域会自动Drop关闭
}
{
// write
println!("Create is write-only mode.\r\n");
let f = File::create("fs.tmp").unwrap();
let mut w = BufWriter::new(f);
_ = w.write("abc hello 历史不会忘记你!".as_bytes());
w.flush().unwrap();
}
{
// Option
println!("open is read-only mode, but option can change it.\r\n");
let mut f = File::options().append(true).open("fs.tmp").unwrap();
_ = f.write("xxxxxxxxxxxxx".as_bytes());
_ = f.flush();
}
{
// dev info
let mut f = File::open("/proc/version").unwrap();
let mut strbuf = String::new();
_ = f.read_to_string(&mut strbuf);
println!("{}", strbuf);
}
}
etc 或 注册表
- Windows 下使用注册表,需要使用第三方库读写
- Linux下使用
/etc/xxx/yyy
, 使用File直接读写。Linux没有注册表的设计,但是gnome环境有 类似注册表的功能gsetting
. https://blog.csdn.net/tongyi04/article/details/127672241