First of all, the line contains the line terminator. You probably want to use trim
(or one of its variants) to ignore this.
Secondly, you do a lot of unnecessary transformations and distributions. Try to avoid this.
Thirdly, to_string
(or at least the last time I checked) is inefficient due to redistribution. You want into_string
.
Fourth, the fastest way to go from String
to &str
is to "intercept" it; if a String
s
, &*s
will borrow it as &str
. This is because String
implements Deref<&str>
; In other words, String
acts as a smart pointer to a borrowed string, allowing it to fade out in a simpler form.
Fifth, if you are not doing something unusual, you can rewrite it as a for
loop using the iterator lines
method.
Sixth, remember that stdin()
actually allocates a new buffered reader every time you call it. Not only that, but characters read in the buffer are not βdiscardedβ in STDIN when creating a new buffer; that the data is simply lost. So you really don't want to be called in a loop. If you need, name it once and save the result in a variable.
So, I get the following:
fn main() { for line in std::io::stdin().lines() {
source share