rust read 100G large file

rust 复制代码
/**

 * Main function that demonstrates memory mapping a large file and accessing its contents.

 *

 * This function:

 * 1. Opens a large virtual disk file (ext4.vhdx)

 * 2. Creates a memory mapping of the file

 * 3. Accesses the mapped content

 * 4. Prints the memory address of the content and its length

 *

 * Returns:

 *   Result<(), Box<dyn std::error::Error>> - Ok on success, or an error boxed as a trait object

 */

fn main() -> Result<(), Box<dyn std::error::Error>> {

    use std::fs::File;

    use doe::memory_address;

    use memmap2::Mmap;

   

    // Open the virtual disk file in read-only mode

    let file = File::open("D:/arch_linux/ext4.vhdx")?;

   

    // Create a memory map of the entire file (unsafe due to potential undefined behavior)

    let mmap = unsafe { Mmap::map(&file)? };

  


    // Get a slice reference to the entire mapped memory

    let content = &mmap[..];

 

    // Print the memory address and length of the mapped content

    println!("memory_address:{:?}", memory_address!(content));

    println!("len:{}", content.len());

   

    Ok(())

}
相关推荐
寻月隐君6 小时前
硬核实战:从零到一,用 Rust 和 Axum 构建高性能聊天服务后端
后端·rust·github
m0_4805026411 小时前
Rust 入门 泛型和特征-特征对象 (十四)
开发语言·后端·rust
RustFS12 小时前
如何用 Rust 对 RustFS MCP Server 进行扩展?
rust
我是前端小学生3 天前
一文梳理Rust语言中的可变结构体实例
rust
Source.Liu3 天前
【unitrix数间混合计算】2.21 二进制整数加法计算(bin_add.rs)
rust
Include everything3 天前
Rust学习笔记(二)|变量、函数与控制流
笔记·学习·rust
Source.Liu3 天前
【unitrix数间混合计算】2.20 比较计算(cmp.rs)
rust
许野平3 天前
Rust:构造函数 new() 如何进行错误处理?
开发语言·后端·rust
许野平3 天前
Rust:专业级错误处理工具 thiserror 详解
rust·error·错误处理·result·thiserror