>> let a: int = from_str(f.read_line().unwrap().as_slice().trim()).unwrap(); > This isn't doing the same thing as the C++ code.
Is there a shorter code to read an integer? On Thu, May 29, 2014 at 2:50 PM, Daniel Micay <[email protected]> wrote: > On 29/05/14 05:22 PM, Oleg Eterevsky wrote: >>>> What would be Rust alternative, except passing the errors all around >>>> parser? Doesn't it grow parser code by at least 50%? >> >>> Haskell has no exception, it has Monads instead. Rust reuses Option and >>> Result (Maybe and Either in Haskell) with great success. >> >> For all my love to Haskell, I wouldn't consider it an example of a >> practical programming language. I mean, there aren't many projects >> written in Haskell of a size larger than GHC. >> >>>> Why not make a trait BaseString and apply all the operations to it? >>> It's "heavier", in several ways. >>> [...] >>> In short, Slice is just a superior alternative. >> >> I agree that it is conceptually simpler. But compare code, required to >> read one integer from a file in Python, C++ and Rust: >> >> a = int(f.read_line()) >> >> int a; >> f >> a; > > Your C++ example isn't checking for an I/O error and isn't even checking > that it succeeded at parsing an integer. > >> let a: int = from_str(f.read_line().unwrap().as_slice().trim()).unwrap(); > > This isn't doing the same thing as the C++ code. The C++ code is reading > an integer from the stream, while this is allocating a string and then > converting. > > An apples to apples comparison would read a string from the C++ API and > convert with std::stoi, or use a similar input API in Rust for streams > to avoid needless string conversions and noise. > > The trim would also be unnecessary if you were using an API comparable > to the one the C++ code is using. > >> Two unwrap's are caused by the lack of exceptions, as_slice is to >> convert String -> str, trim is because from_str doesn't skip >> whitespace (I suppose, the last one is non-essential). > > > _______________________________________________ > 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
