[rust-dev] write_str error

Mic mictadlo at gmail.com
Sun Apr 8 18:17:28 PDT 2012

Thank you it is working.

However, why does Rust require to write '[io::create, io::truncate]' and
not like in Python's node in open(filename, mode). The first argument is a
string containing the filename. The second argument is another string
containing a few characters describing the way in which the file will be
used. mode can be 'r' when the file will only be read, 'w' for only writing
(an existing file with the same name will be erased), and 'a' opens the
file for appending; any data written to the file is automatically added to
the end. 'r+' opens the file for both reading and writing. The mode
argument is optional; 'r' will be assumed if it’s omitted.

On Windows, 'b' appended to the mode opens the file in binary mode, so
there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a
distinction between text and binary files; the end-of-line characters in
text files are automatically altered slightly when data is read or written.
This behind-the-scenes modification to file data is fine for ASCII text
files, but it’ll corrupt binary data like that in JPEG or EXE files. Be
very careful to use binary mode when reading and writing such files. On
Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it
platform-independently for all binary files.

On Mon, Apr 9, 2012 at 6:45 AM, Brian Anderson <banderson at mozilla.com>wrote:

> **
> On 04/07/2012 08:58 PM, Mic wrote:
> Hi,
> I am getting the following errors:
>  $ rustc csv_create.rs
> csv_create.rs:17:1: 17:14 error: attempted access of field write_str on
> type core::io::writer, but no public field or method with that name was
> found
> csv_create.rs:17        rdr.write_str("aaa, bbb,ccc ,
> ddd,eee,fff,ggg,hhh,iii,jjj,kkk,lll,mmm,nnn\n");
>                                   ^~~~~~~~~~~~~
>  csv_create.rs:17:1: 17:78 error: mismatched types: expected function or
> native function but found _|_
> csv_create.rs:17        rdr.write_str("aaa, bbb,ccc ,
> ddd,eee,fff,ggg,hhh,iii,jjj,kkk,lll,mmm,nnn\n");
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>  with the following code:
>  import io::reader_util;
> import vec::map;
>  fn main(args: [str]) {
>      let r = io::file_writer("csv.csv" , [io::create, io::truncate]); //
> r is result<reader, err_str>
>      if result::is_failure(r) {
>         fail result::get_err(r);
>     }
>      let rdr = result::get(r);
>      let count = 0;
>     while true {
>          if count == 4000000 { break; }
>         rdr.write_str("aaa, bbb,ccc ,
> ddd,eee,fff,ggg,hhh,iii,jjj,kkk,lll,mmm,nnn\n");
>         count += 1;
>     }
>  }
>  What did I do wrong and would it possible to rewrite the while loop with
> for loop?
> I believe the problem is that you are using a writer type but have
> imported a reader impl. if you add an `import io::writer_util;` statement
> then it will get farther.
> The most concise way to write your while loop would be using
> `iter::repeat` which just executes a function a specific number of times,
> like `iter::repeat(4000000) {|| ... }`. Sadly `iter::repeat` can't be used
> in a for loop yet. Our iteration strategy still needs an overhaul to be
> compatible with `for`.
> -Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120409/0a030354/attachment.html>

More information about the Rust-dev mailing list