file - 在 Rust 中以读写模式打开文件
问题描述
在 Rust 中,如何打开文件进行读写?File::open()
是只读的,而File::create()
声明是只写的(并且还会创建不是我想要的文件)。
解决方案
从 Rust 1.58(我写这篇文章的下一个版本)开始,你可以这样做:
use std::fs::File;
let mut file = File::options()
.read(true)
.write(true)
.open("foo.txt")?;
在旧版本上,您可以使用OpenOptions
struct 打开文件。它与上面的代码相同,但名称更奇怪。
use std::fs::OpenOptions;
let mut file = OpenOptions::new()
.read(true)
.write(true)
.open("foo.txt")?;
推荐阅读
- python - 如何避免打印包含“无”作为值的某些数据?
- ionic-framework - 如何在 cordova 插件中添加离子 devApp 支持
- javascript - TripleDES 加密 - c# 和 javascript 的区别
- c# - 具有特殊字符的文件夹中的 c#.WebApi 解决方案的依赖性问题
- java - 如何在 libGDX 中以相反的方式射击子弹
- c# - 是否有一个选项可以在不制作“GlobalSuppressions.cs”文件的情况下停用样式警察规则?
- asp.net-core - 为什么在使用 IIS Express 而不是 Kestrel 时 Active Directory 可以与 Visual Studio MVC 模板一起使用?
- raspberry-pi3 - 当光标放在 wxTextCtrl 上时,调用屏幕键盘的最佳方法是什么?
- javascript - 有没有办法使用 UI Kitten 将主色设置为 Layout 组件的背景色?
- c# - 如何发送延续令牌来查询天蓝色的时间序列见解?