Right now the file I/O isn't quite what I think we want it to be in
the end, but otherwise that is roughly the code which you'll want to
write. It could be slightly shortened like this:
use std::rt::io::Writer;
use std::rt::io::file::FileInfo;
use std::rt::io;
let mut stream = Path::new("test.txt").open_writer(io::Create);
writeln!(&mut stream as &mut Writer, "test {}, {}, {aa}", 1, 3.0, aa=2);
Or at least you could write it as such if you didn't care about
errors. Right now there's "impl<T: Reader> Reader for Option<T>" so
there's no need to inspect the return value of open_writer.
There are a few planned changes to this code which I would like to put
into place, however:
1. std::rt::io::Writer belongs in the prelude
2. There should be a std::rt::io::File which wraps many common
operations like opening a file, creating a file, etc, which doesn't
require importing a trait from std::rt::io::file along with importing
std::rt::io.
3. We should have object coercion so "&mut stream as &mut Writer"
isn't necessary, but rather "&mut stream" is sufficient.
With these changes, the code would look like:
use std::rt::io::File;
let mut stream = File::create("test.txt");
writeln!(&mut stream, "test {}, {}, {aa}", 1, 3.0, aa=2);
Which I think looks a lot better than before! I would love to not have
to write `&mut stream` or have something like `stream.write!(foo, bar,
baz)`, but I haven't thought much about changing the write! macro.
On Wed, Oct 23, 2013 at 1:22 AM, Rémi Fontan <[email protected]> wrote:
> Hi,
>
> I'm learning how to open a file and write simple text data in it. I managed
> something to work but was wondering whether I had to cast my filestream as a
> writer:
>
>
> use std::path::Path;
> use std::rt::io::Writer;
> use std::rt::io::file::open;
> use std::rt::io::{Create, ReadWrite};
>
> let p = &Path("test.txt");
> let mut stream = match open(p, Create, ReadWrite) {
> Some(s) => s,
> None => fail!("whoops! I'm sure this raised, anyways..")
> };
>
> writeln!(&mut stream as &mut Writer, "test {}, {}, {aa}", 1, 3.0, aa=2);
>
>
> Is that the simplest way to open a file and write in it? I find the "&mut
> stream as &mut Writer" a bit surprising.
>
> cheers,
>
> Rémi
>
>
>
>
> _______________________________________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/rust-dev
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev