user input

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

user input

Renato Lenzi
I would like to manage user input for example by storing it in a string. I found this solution:

use std::io::buffered::BufferedReader;
use std::io::stdin;

fn main()
{
    let mut stdin = BufferedReader::new(stdin());
    let mut s1 = stdin.read_line().unwrap_or(~"nothing");
    print(s1);
 }

It works but it seems (to me) a bit verbose, heavy... is there a cheaper way to do this simple task?

Thx.

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Alex Crichton
We do indeed want to make common tasks like this fairly lightweight,
but we also strive to require that the program handle possible error
cases. Currently, the code you have shows well what one would expect
when reading a line of input. On today's master, you might be able to
shorten it slightly to:

    use std::io::{stdin, BufferedReader};

    fn main() {
        let mut stdin = BufferedReader::new(stdin());
        for line in stdin.lines() {
            println!("{}", line);
        }
    }

I'm curious thought what you think is the heavy/verbose aspects of
this? I like common patterns having shortcuts here and there!

On Sat, Feb 8, 2014 at 3:06 PM, Renato Lenzi <[hidden email]> wrote:

> I would like to manage user input for example by storing it in a string. I
> found this solution:
>
> use std::io::buffered::BufferedReader;
> use std::io::stdin;
>
> fn main()
> {
>     let mut stdin = BufferedReader::new(stdin());
>     let mut s1 = stdin.read_line().unwrap_or(~"nothing");
>     print(s1);
>  }
>
> It works but it seems (to me) a bit verbose, heavy... is there a cheaper way
> to do this simple task?
>
> Thx.
>
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev
>
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Lee Braiden
On 08/02/14 23:35, Alex Crichton wrote:
> I'm curious thought what you think is the heavy/verbose aspects of
> this? I like common patterns having shortcuts here and there!

When reading the original post, it did occur to me that there should
probably be a readln() equivalent of println(), if only as a special
case to keep very simple programs short, or to help people "step up"
from Hello, World.  I was concerned that it might interfere with the
ability to create a BufferedReader around stdin, but if BufferedReader
is a self-contained object/struct/trait, then I guess it should "just work".
--
Lee

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Renato Lenzi
In reply to this post by Alex Crichton
I believe that reading a string from console should be considered one of the simplest task to perform.....
Ok i do not pretend a sintax like

s1 = input()

ala Python but perhaps somehting like 

string s1 = Console.Readline();

as in C# mode would be sufficient for a basic input control... sure, it's more readable... but actually the thing that puzzles me is that you cannot  write something like:

let mut s1 = stdin.read_line();

cause the compiler complains (why?)....it's a matter of taste, of course.

Regards


On Sun, Feb 9, 2014 at 12:35 AM, Alex Crichton <[hidden email]> wrote:
We do indeed want to make common tasks like this fairly lightweight,
but we also strive to require that the program handle possible error
cases. Currently, the code you have shows well what one would expect
when reading a line of input. On today's master, you might be able to
shorten it slightly to:

    use std::io::{stdin, BufferedReader};

    fn main() {
        let mut stdin = BufferedReader::new(stdin());
        for line in stdin.lines() {
            println!("{}", line);
        }
    }

I'm curious thought what you think is the heavy/verbose aspects of
this? I like common patterns having shortcuts here and there!

On Sat, Feb 8, 2014 at 3:06 PM, Renato Lenzi <[hidden email]> wrote:
> I would like to manage user input for example by storing it in a string. I
> found this solution:
>
> use std::io::buffered::BufferedReader;
> use std::io::stdin;
>
> fn main()
> {
>     let mut stdin = BufferedReader::new(stdin());
>     let mut s1 = stdin.read_line().unwrap_or(~"nothing");
>     print(s1);
>  }
>
> It works but it seems (to me) a bit verbose, heavy... is there a cheaper way
> to do this simple task?
>
> Thx.
>
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev
>


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Liigo Zhuang
In reply to this post by Alex Crichton


2014年2月9日 上午7:35于 "Alex Crichton" <[hidden email]>写道:
>
> We do indeed want to make common tasks like this fairly lightweight,
> but we also strive to require that the program handle possible error
> cases. Currently, the code you have shows well what one would expect
> when reading a line of input. On today's master, you might be able to
> shorten it slightly to:
>
>     use std::io::{stdin, BufferedReader};
>
>     fn main() {
>         let mut stdin = BufferedReader::new(stdin());
>         for line in stdin.lines() {
>             println!("{}", line);
>         }
>     }
>
> I'm curious thought what you think is the heavy/verbose aspects of
> this? I like common patterns having shortcuts here and there!
>

This is not a common pattern for stdin. Programs often need process something when user press return key, immediately. So read one line is more useful than read multiple lines, at least for stdin. I agree to need stdin.readln or read_line.

> On Sat, Feb 8, 2014 at 3:06 PM, Renato Lenzi <[hidden email]> wrote:
> > I would like to manage user input for example by storing it in a string. I
> > found this solution:
> >
> > use std::io::buffered::BufferedReader;
> > use std::io::stdin;
> >
> > fn main()
> > {
> >     let mut stdin = BufferedReader::new(stdin());
> >     let mut s1 = stdin.read_line().unwrap_or(~"nothing");
> >     print(s1);
> >  }
> >
> > It works but it seems (to me) a bit verbose, heavy... is there a cheaper way
> > to do this simple task?
> >
> > Thx.
> >
> > _______________________________________________
> > Rust-dev mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/rust-dev
> >
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Huon Wilson
There is read_line: http://static.rust-lang.org/doc/master/std/io/trait.Buffer.html#method.read_line

     use std::io::{stdin, BufferedReader};

     fn main() {
         let mut stdin = BufferedReader::new(stdin());
         let line = stdin.read_line().unwrap();
         println!("{}", line);
     }



Huon

On 09/02/14 11:44, Liigo Zhuang wrote:


2014年2月9日 上午7:35于 "Alex Crichton" <[hidden email]>写道:
>
> We do indeed want to make common tasks like this fairly lightweight,
> but we also strive to require that the program handle possible error
> cases. Currently, the code you have shows well what one would expect
> when reading a line of input. On today's master, you might be able to
> shorten it slightly to:
>
>     use std::io::{stdin, BufferedReader};
>
>     fn main() {
>         let mut stdin = BufferedReader::new(stdin());
>         for line in stdin.lines() {
>             println!("{}", line);
>         }
>     }
>
> I'm curious thought what you think is the heavy/verbose aspects of
> this? I like common patterns having shortcuts here and there!
>

This is not a common pattern for stdin. Programs often need process something when user press return key, immediately. So read one line is more useful than read multiple lines, at least for stdin. I agree to need stdin.readln or read_line.

> On Sat, Feb 8, 2014 at 3:06 PM, Renato Lenzi <[hidden email]> wrote:
> > I would like to manage user input for example by storing it in a string. I
> > found this solution:
> >
> > use std::io::buffered::BufferedReader;
> > use std::io::stdin;
> >
> > fn main()
> > {
> >     let mut stdin = BufferedReader::new(stdin());
> >     let mut s1 = stdin.read_line().unwrap_or(~"nothing");
> >     print(s1);
> >  }
> >
> > It works but it seems (to me) a bit verbose, heavy... is there a cheaper way
> > to do this simple task?
> >
> > Thx.
> >
> > _______________________________________________
> > Rust-dev mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/rust-dev
> >
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev



_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Sean McArthur
let in = readln!() ?

macro_rules! readln(
  () => ({
    let mut stdin = ::std::io::BufferedReader::new(::std::io::stdin());
    stdin.read_line().unwrap()
  })
)




On Sat, Feb 8, 2014 at 4:48 PM, Huon Wilson <[hidden email]> wrote:
There is read_line: http://static.rust-lang.org/doc/master/std/io/trait.Buffer.html#method.read_line


     use std::io::{stdin, BufferedReader};

     fn main() {
         let mut stdin = BufferedReader::new(stdin());
         let line = stdin.read_line().unwrap();
         println!("{}", line);
     }



Huon


On 09/02/14 11:44, Liigo Zhuang wrote:


2014年2月9日 上午7:35于 "Alex Crichton" <[hidden email]>写道:
>
> We do indeed want to make common tasks like this fairly lightweight,
> but we also strive to require that the program handle possible error
> cases. Currently, the code you have shows well what one would expect
> when reading a line of input. On today's master, you might be able to
> shorten it slightly to:
>
>     use std::io::{stdin, BufferedReader};
>
>     fn main() {
>         let mut stdin = BufferedReader::new(stdin());
>         for line in stdin.lines() {
>             println!("{}", line);
>         }
>     }
>
> I'm curious thought what you think is the heavy/verbose aspects of
> this? I like common patterns having shortcuts here and there!
>

This is not a common pattern for stdin. Programs often need process something when user press return key, immediately. So read one line is more useful than read multiple lines, at least for stdin. I agree to need stdin.readln or read_line.

> On Sat, Feb 8, 2014 at 3:06 PM, Renato Lenzi <[hidden email]> wrote:
> > I would like to manage user input for example by storing it in a string. I
> > found this solution:
> >
> > use std::io::buffered::BufferedReader;
> > use std::io::stdin;
> >
> > fn main()
> > {
> >     let mut stdin = BufferedReader::new(stdin());
> >     let mut s1 = stdin.read_line().unwrap_or(~"nothing");
> >     print(s1);
> >  }
> >
> > It works but it seems (to me) a bit verbose, heavy... is there a cheaper way
> > to do this simple task?
> >
> > Thx.
> >
> > _______________________________________________
> > Rust-dev mailing list
> > [hidden email]
> > https://mail.mozilla.org/listinfo/rust-dev
> >
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev



_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev



_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Patrick Walton-2
In reply to this post by Alex Crichton
On 2/8/14 3:35 PM, Alex Crichton wrote:

> We do indeed want to make common tasks like this fairly lightweight,
> but we also strive to require that the program handle possible error
> cases. Currently, the code you have shows well what one would expect
> when reading a line of input. On today's master, you might be able to
> shorten it slightly to:
>
>      use std::io::{stdin, BufferedReader};
>
>      fn main() {
>          let mut stdin = BufferedReader::new(stdin());
>          for line in stdin.lines() {
>              println!("{}", line);
>          }
>      }
>
> I'm curious thought what you think is the heavy/verbose aspects of
> this? I like common patterns having shortcuts here and there!

Is there any way we can get rid of the need to create a buffered reader?
It feels too enterprisey.

Patrick

_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Fwd: user input

Renato Lenzi


Always talking about read & write i noticed another interesting thing:

use std::io::buffered::BufferedReader;
use std::io::stdin;

fn main()
{
    print!("Insert your name: ");
    let mut stdin = BufferedReader::new(stdin());
    let s1 = stdin.read_line().unwrap_or(~"nothing");
    print!("Welcome, {}", s1);
}

when i run this simple code the output "Insert your name" doesn't appear on the screen... only after typing and entering a string the whole output jumps out... am i missing some "flush" (ala Fantom) or similar? I am using Rust 0.9 on W7.


On Sun, Feb 9, 2014 at 2:40 AM, Patrick Walton <[hidden email]> wrote:
On 2/8/14 3:35 PM, Alex Crichton wrote:
We do indeed want to make common tasks like this fairly lightweight,
but we also strive to require that the program handle possible error
cases. Currently, the code you have shows well what one would expect
when reading a line of input. On today's master, you might be able to
shorten it slightly to:

     use std::io::{stdin, BufferedReader};

     fn main() {
         let mut stdin = BufferedReader::new(stdin());
         for line in stdin.lines() {
             println!("{}", line);
         }
     }

I'm curious thought what you think is the heavy/verbose aspects of
this? I like common patterns having shortcuts here and there!

Is there any way we can get rid of the need to create a buffered reader? It feels too enterprisey.

Patrick


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev



_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: user input

Matthieu Monrocq



On Sun, Feb 9, 2014 at 12:15 PM, Renato Lenzi <[hidden email]> wrote:


Always talking about read & write i noticed another interesting thing:

use std::io::buffered::BufferedReader;
use std::io::stdin;

fn main()
{
    print!("Insert your name: ");
    let mut stdin = BufferedReader::new(stdin());
    let s1 = stdin.read_line().unwrap_or(~"nothing");
    print!("Welcome, {}", s1);
}

when i run this simple code the output "Insert your name" doesn't appear on the screen... only after typing and entering a string the whole output jumps out... am i missing some "flush" (ala Fantom) or similar? I am using Rust 0.9 on W7.

Ah, that's interesting. In most languages whenever you ask for user input (read on stdin) it automatically triggers a flush on stdout and stderr to avoid this uncomfortable situation.

I suppose it would not be took difficult to incorporate this in Rust.

-- Matthieu.
 


On Sun, Feb 9, 2014 at 2:40 AM, Patrick Walton <[hidden email]> wrote:
On 2/8/14 3:35 PM, Alex Crichton wrote:
We do indeed want to make common tasks like this fairly lightweight,
but we also strive to require that the program handle possible error
cases. Currently, the code you have shows well what one would expect
when reading a line of input. On today's master, you might be able to
shorten it slightly to:

     use std::io::{stdin, BufferedReader};

     fn main() {
         let mut stdin = BufferedReader::new(stdin());
         for line in stdin.lines() {
             println!("{}", line);
         }
     }

I'm curious thought what you think is the heavy/verbose aspects of
this? I like common patterns having shortcuts here and there!

Is there any way we can get rid of the need to create a buffered reader? It feels too enterprisey.

Patrick


_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev



_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev



_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: user input

Maciej Piechotka
In reply to this post by Sean McArthur
On Sat, 2014-02-08 at 17:23 -0800, Sean McArthur wrote:

> let in = readln!() ?
>
> macro_rules! readln(
>   () => ({
>     let mut stdin
> = ::std::io::BufferedReader::new(::std::io::stdin());
>     stdin.read_line().unwrap()
>   })
> )
>
Unless I read the source incorrectly that won't work if the input is not
line buffered. The filling of the buffer can exceed the length of line -
it can be as large as 64ki. You then read a line and then discard all of
the buffered data on exit.

In many circumstances if might work as by default stdin is line buffered
(at least in C) but the program might start misbehave when it would be
switched from terminal input to pipe input.




_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: user input

Alex Crichton
In reply to this post by Matthieu Monrocq
> Is there any way we can get rid of the need to create a buffered reader? It feels too enterprisey.

There's really no way to efficiently define a `read_line` method on a
reader that doesn't have an underlying buffer. For that reason, I
think that we'll always require that the stream be buffered somehow.

Despite that, we could return a buffered stdin by default. We could
also have task-local stdin handles which hide the ability to be
buffered. This will not play nicely at all with multiple tasks reading
stdin, but I don't think the current solution plays very nicely so I'd
be fine glossing over that use case.

> Ah, that's interesting. In most languages whenever you ask for user input (read on stdin) it automatically triggers a flush on stdout and stderr

That's a good point that I hadn't thought of. If we go towards a
task-local stdin then we could make the read methods on the local
stdin handle flush the local output handles, which would probably
solve this problem.


On Sun, Feb 9, 2014 at 3:26 AM, Matthieu Monrocq
<[hidden email]> wrote:

>
>
>
> On Sun, Feb 9, 2014 at 12:15 PM, Renato Lenzi <[hidden email]> wrote:
>>
>>
>>
>> Always talking about read & write i noticed another interesting thing:
>>
>> use std::io::buffered::BufferedReader;
>> use std::io::stdin;
>>
>> fn main()
>> {
>>     print!("Insert your name: ");
>>     let mut stdin = BufferedReader::new(stdin());
>>     let s1 = stdin.read_line().unwrap_or(~"nothing");
>>     print!("Welcome, {}", s1);
>> }
>>
>> when i run this simple code the output "Insert your name" doesn't appear
>> on the screen... only after typing and entering a string the whole output
>> jumps out... am i missing some "flush" (ala Fantom) or similar? I am using
>> Rust 0.9 on W7.
>
>
> Ah, that's interesting. In most languages whenever you ask for user input
> (read on stdin) it automatically triggers a flush on stdout and stderr to
> avoid this uncomfortable situation.
>
> I suppose it would not be took difficult to incorporate this in Rust.
>
> -- Matthieu.
>
>>
>>
>>
>> On Sun, Feb 9, 2014 at 2:40 AM, Patrick Walton <[hidden email]>
>> wrote:
>>>
>>> On 2/8/14 3:35 PM, Alex Crichton wrote:
>>>>
>>>> We do indeed want to make common tasks like this fairly lightweight,
>>>> but we also strive to require that the program handle possible error
>>>> cases. Currently, the code you have shows well what one would expect
>>>> when reading a line of input. On today's master, you might be able to
>>>> shorten it slightly to:
>>>>
>>>>      use std::io::{stdin, BufferedReader};
>>>>
>>>>      fn main() {
>>>>          let mut stdin = BufferedReader::new(stdin());
>>>>          for line in stdin.lines() {
>>>>              println!("{}", line);
>>>>          }
>>>>      }
>>>>
>>>> I'm curious thought what you think is the heavy/verbose aspects of
>>>> this? I like common patterns having shortcuts here and there!
>>>
>>>
>>> Is there any way we can get rid of the need to create a buffered reader?
>>> It feels too enterprisey.
>>>
>>> Patrick
>>>
>>>
>>> _______________________________________________
>>> Rust-dev mailing list
>>> [hidden email]
>>> https://mail.mozilla.org/listinfo/rust-dev
>>
>>
>>
>>
>> _______________________________________________
>> Rust-dev mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/rust-dev
>>
>
>
> _______________________________________________
> Rust-dev mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/rust-dev
>
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: user input

Patrick Walton
Yeah, I had similar thoughts around putting an auto-buffered stdin in TLS. I think it's a good idea if for no other reason than first impressions.

Patrick

Alex Crichton <[hidden email]> wrote:
> Is there any way we can get rid of the need to create a buffered reader? It feels too enterprisey.

There's really no way to efficiently define a `read_line` method on a
reader that doesn't have an underlying buffer. For that reason, I
think that we'll always require that the stream be buffered somehow.

Despite that, we could return a buffered stdin by default. We could
also have task-local stdin handles which hide the ability to be
buffered. This will not play nicely at all with multiple tasks reading
stdin, but I don't think the current solution plays very nicely so I'd
be fine glossing over that use case.

Ah, that's interesting. In most languages whenever you ask for user input (read on stdin) it automatically triggers a flush on stdout and stderr

That's a good point that I hadn't thought of. If we go towards a
task-local stdin then we could make the read methods on the local
stdin handle flush the local output handles, which would probably
solve this problem.


On Sun, Feb 9, 2014 at 3:26 AM, Matthieu Monrocq
<[hidden email]> wrote:



On Sun, Feb 9, 2014 at 12:15 PM, Renato Lenzi <[hidden email]> wrote:



Always talking about read & write i noticed another interesting thing:

use std::io::buffered::BufferedReader;
use std::io::stdin;

fn main()
{
print!("Insert your name: ");
let mut stdin = BufferedReader::new(stdin());
let s1 = stdin.read_line().unwrap_or(~"nothing");
print!("Welcome, {}", s1);
}

when i run this simple code the output "Insert your name" doesn't appear
on the screen... only after typing and entering a string the whole output
jumps out... am i missing some "flush" (ala Fantom) or similar? I am using
Rust 0.9 on W7.


Ah, that's interesting. In most languages whenever you ask for user input
(read on stdin) it automatically triggers a flush on stdout and stderr to
avoid this uncomfortable situation.

I suppose it would not be took difficult to incorporate this in Rust.

-- Matthieu.




On Sun, Feb 9, 2014 at 2:40 AM, Patrick Walton <[hidden email]>
wrote:

On 2/8/14 3:35 PM, Alex Crichton wrote:

We do indeed want to make common tasks like this fairly lightweight,
but we also strive to require that the program handle possible error
cases. Currently, the code you have shows well what one would expect
when reading a line of input. On today's master, you might be able to
shorten it slightly to:

use std::io::{stdin, BufferedReader};

fn main() {
let mut stdin = BufferedReader::new(stdin());
for line in stdin.lines() {
println!("{}", line);
}
}

I'm curious thought what you think is the heavy/verbose aspects of
this? I like common patterns having shortcuts here and there!


Is there any way we can get rid of the need to create a buffered reader?
It feels too enterprisey.

Patrick




Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev






Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev





Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev



Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev

--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
_______________________________________________
Rust-dev mailing list
[hidden email]
https://mail.mozilla.org/listinfo/rust-dev