Skip to content

Doc's example code for BufferedWriter is wrong and also generate warnings #18197

@jameschurchman

Description

@jameschurchman

Struct std::io::BufferedWriter has the example code :

use std::io::{BufferedWriter, File};

let file = File::open(&Path::new("message.txt"));
let mut writer = BufferedWriter::new(file);

writer.write_str("hello, world");
writer.flush();

But this code opens the file as read only, it should open it for writing or creating a new file, for example :

let file = File::create(&Path::new("message.txt"));

It also generates two warnings :

buff_writer.rs:7:2: 7:35 warning: unused result which must be used, #[warn(unused_must_use)] on by default
buff_writer.rs:7    writer.write_str("hello, world");
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
buff_writer.rs:8:2: 8:17 warning: unused result which must be used, #[warn(unused_must_use)] on by default
buff_writer.rs:8    writer.flush();

by unwrap()ing both these remove the errors, and in the case where File::open is used to successfully crash the sample app when attempting to append to a read only file.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions