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(())

}
相关推荐
布列瑟农的星空4 小时前
前端都能看懂的Rust入门教程(三)——控制流语句
前端·后端·rust
Andrew_Ryan10 小时前
用 Rust 构建高性能 LiteLLM 客户端:支持流式与非流式调用
rust
魔力军11 小时前
Rust学习Day3: 3个小demo实现
java·学习·rust
Smart-Space12 小时前
htmlbuilder - rust灵活构建html
rust·html
魔力军12 小时前
Rust学习Day2: 变量与可变性、数据类型和函数和控制流
开发语言·学习·rust
暴躁小师兄数据学院1 天前
【WEB3.0零基础转行笔记】Rust编程篇-第一讲:课程简介
rust·web3·区块链·智能合约
Hello.Reader1 天前
Rocket Fairings 实战把全局能力做成“结构化中间件”
中间件·rust·rocket
Andrew_Ryan1 天前
rust arena 内存分配
rust
Andrew_Ryan1 天前
深入理解 Rust 内存管理:基于 typed_arena 的指针操作实践
rust
微小冷2 天前
Rust异步编程详解
开发语言·rust·async·await·异步编程·tokio