[rust-dev] read file line by line

Grahame Bowland grahame at angrygoats.net
Fri Apr 6 19:08:56 PDT 2012


Hey

You want str::split_char(line), not line.split_char().

As a tip, I'd just do a 'git grep' in a checkout of rust when you hit
problems like this. It's what I do when I can't figure something out -
thanks to the test coverage, you can find example code for pretty much
everything in the standard/core libraries.

Cheers
Grahame

On 7 April 2012 09:48, Mic <mictadlo at gmail.com> wrote:

> Hi
> I have trouble to compile the following code:
>
> import io::reader_util;
> import vec::map;
>
> fn main(args: [str]) {
>
>     let r = io::file_reader(args[1]); // r is result<reader, err_str>
>     if result::failure(r) {
>         fail result::get_err(r);
>     }
>
>     let rdr = result::get(r);
>
>     while !rdr.eof() {
>         let line = rdr.read_line();
>         io::println(line);
>         if str::len(line) != 0u {
>             let parts = vec::map(line.split_char(',')) {|s|
>                 str::trim(s)
>             };
>         }
>
>     }
> }
>
> and got the errors:
> $ rustc csv.rs
> csv.rs:17:33: 17:48 error: attempted access of field split_char on type
> str, but no public field or method with that name was found
> csv.rs:17             let parts = vec::map(line.split_char(',')) {|s|
>
>  ^~~~~~~~~~~~~~~
> csv.rs:17:33: 17:53 error: the type of this value must be known in this
> context
> csv.rs:17             let parts = vec::map(line.split_char(',')) {|s|
>
>  ^~~~~~~~~~~~~~~~~~~~
>
> What did I do wrong?
>
>
> On Fri, Apr 6, 2012 at 2:58 AM, Eric Holk <eric.holk at gmail.com> wrote:
>
>> On Wed, Apr 4, 2012 at 4:39 PM, Brian Anderson <banderson at mozilla.com>wrote:
>>
>>> On 04/04/2012 08:17 AM, Masklinn wrote:
>>>
>>>> On 4 avr. 2012, at 17:09, Eric Holk<eric.holk at gmail.com>  wrote:
>>>>
>>>>> In Rust, you can do something like this instead:
>>>>>
>>>>>    let parts = vec::map([" a", "b ", " c ", "d"]) {|s|
>>>>>        str::trim(s)
>>>>>    };
>>>>>
>>>> Isn't it possible to pass str::trim directly to vec::map? It the
>>>> indirection through the block really needed?
>>>>
>>>
>>> In this case I believe the block isn't necessary, but in many situations
>>> it is so I've gotten used to just always using it (sadly). The reason is
>>> because generic functions always take arguments by reference while
>>> functions on scalars take their arguments by value, so composing them isn't
>>> possible without an adapter between them. Strings are passed by reference
>>> though so this example should work without the extra block.
>>>
>>>
>> I originally wrote this without the extra block, and the compiler
>> complained.
>>
>> I was also trying to use the extension methods (`[" a", "b ", " c ",
>> "d"].map(str::trim)`) instead, but the compiler was having trouble finding
>> them. I'm pretty sure I was using the rustc I had just pulled from Github.
>>
>> -Eric
>>
>>
>> _______________________________________________
>> Rust-dev mailing list
>> Rust-dev at mozilla.org
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20120407/e0fb20ac/attachment-0001.html>


More information about the Rust-dev mailing list