读写
使用内存映射,随机访问文件
这个Mmap::map函数假定内存映射后面的文件,不会同时被另一个进程修改,或者竞争条件发生。
extern crate memmap;use memmap::Mmap;use std::fs::File;use std::io::{Write, Error};fn main() -> Result<(), Error> {write!(File::create("content.txt")?, "My hovercraft is full of eels!")?;let file = File::open("content.txt")?;let map = unsafe { Mmap::map(&file)? };let random_indexes = [0, 1, 2, 19, 22, 10, 11, 29];assert_eq!(&map[3..13], b"hovercraft");let random_bytes: Vec<u8> = random_indexes.iter().map(|&idx| map[idx]).collect();assert_eq!(&random_bytes[..], b"My loaf!");Ok(())}
