Rust 实战练习 - 2. OS,IO,Platform(注册表,/etc)

目标:

  • 检测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 或 注册表

相关推荐
好家伙VCC22 分钟前
区块链双向支付通道实战:从签名到结算
java·后端·区块链·asp.net
知识的宝藏30 分钟前
Xpaht self::div 轴语法
开发语言
keykey6.31 分钟前
卷积神经网络(CNN):让AI学会“看“
开发语言·人工智能·深度学习·机器学习
IsJunJianXin33 分钟前
谷歌搜索cookie NID逆向生成
开发语言·python·google搜索·sgss·nid-cookie·算法生成nid·google-cookie
weikecms43 分钟前
美团霸王餐报名API接口
java·开发语言
繁星蓝雨1 小时前
C++中对比pragma once和ifndef的使用区别
开发语言·c++·ifndef·头文件·pragma once
.千余1 小时前
【C++】C++手写Vector容器:从底层源码模拟实现
开发语言·c++·经验分享·笔记·学习
我登哥MVP1 小时前
Spring Boot 从“会用”到“精通”:参数解析原理
java·spring boot·后端·spring·servlet·maven·intellij-idea
a诠释淡然1 小时前
C++ vs Rust:哪个更适合你的下一个项目?
开发语言·c++·rust
meilindehuzi_a1 小时前
深入理解 JavaScript 执行机制:从编译阶段到调用栈底层实现
开发语言·javascript·ecmascript