Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upUser-friendly input macros. #67
Conversation
This comment has been minimized.
This comment has been minimized.
|
See also issue 6220. |
This comment has been minimized.
This comment has been minimized.
|
cc @lifthrasiir (since you wrote a (non-procedural!) macro for this.) |
This comment has been minimized.
This comment has been minimized.
|
@huonw Actually I have my own experimental syntax extension named read.rs, and an associated draft design document. In my opinion, the most problematic aspect of such syntax extension is how to return the read values if any. For example, if |
This comment has been minimized.
This comment has been minimized.
aepsil0n
commented
May 5, 2014
|
I find it very weird to have this pseudo-initialization code before the call to |
This comment has been minimized.
This comment has been minimized.
|
@aepsil0n this is pretty much what I think you could probably get away without the initialisation too, e.g.,
I want to avoid returning a value wrapped in an |
This comment has been minimized.
This comment has been minimized.
aepsil0n
commented
May 5, 2014
|
Would this work? let (x, y): (f64, str) = scanln!("{} {}");It's clear from the context, but I don't know how powerful the macro system is. |
This comment has been minimized.
This comment has been minimized.
This comment IMO ties into questions raised about the More specifically: there is a viewpoint put forward in the comments for that ticket that if you want to handle errors printing to stdout, you should be using
|
This comment has been minimized.
This comment has been minimized.
|
@aepsil0n it might. I'm also not so clear on if the macro system is powerful enough to do that. It works for @pnkfelix yeah, I agree with that viewpoint on |
This comment has been minimized.
This comment has been minimized.
|
I disagree with several of the assumptions made in this RFC. scan should not be perfectly symmetrical with the way In my experience, there are only two kinds of input:
... and I've found that it makes for better error messages if I implement token-based input on top of line-based input. In my C++ codebase, I currently have the following kinds of splitters:
|
This comment has been minimized.
This comment has been minimized.
|
@o11c I think you are right for a general purpose IO library. But I am proposing scan as a 'toy' IO library. The kind if thing which is suited for programming exercises (in the tutorials, or for a university course, for example) or programming competitions. We just want ease of use, really. Robustness, extensibility, and efficiency are not primary as they would be for a real-world IO library. |
This comment has been minimized.
This comment has been minimized.
|
@nick29581 I completely disagree that a toy library is a good idea - and that is certainly not something that parallels It's no harder to use my libraries than to use scanf, and it's much safer. |
This comment has been minimized.
This comment has been minimized.
|
@o11c I guess we just have to disagree. I can see the merit in your approach though. My feeling is that when teaching, you should teach one thing at a time, so when teaching about IO, you should teach the good stuff that matters, but when teaching something else where IO is peripheral and you just want to get some input to make a fun exercise, then you just want something that is as simple as possible. Any boilerplate at all is a distraction. There are certainly pros and cons for matching scan with print, they are not doing the same thing, but then the symmetry is appealing. I'm not sure from your library description how they are used, but it seems more complex than scanf, which is just a single function call. In the same way that println! exists just to do output in exercies/prototyping/debugging, I think there should be something similarly simple for input. |
This comment has been minimized.
This comment has been minimized.
Nope. All of the below linked code is replacing calls to
For
(obviously in Rust these would be a trait implementation rather than overloaded functions) Links to how simple or complicated
Also, the human-facing functions which are only called the same file they're defined in:
I haven't linked |
This comment has been minimized.
This comment has been minimized.
nielsle
commented
May 19, 2014
|
Perhaps:
|
This comment has been minimized.
This comment has been minimized.
|
This was discussed in today's meeting and it was decided that a feature such as this should bake in a library before being accepted into the main repo, so I'm going to close this for now. This would be a nice feature to have though! |
alexcrichton
closed this
Jun 17, 2014
This comment has been minimized.
This comment has been minimized.
uazu
commented
Jun 18, 2014
|
I don't know whether you've considered the possibility of matching/parsing either all or nothing (but nothing in between). If the whole thing matches, then all variables are assigned. Otherwise all variables are unset (or nil'd/zero'd) and all the characters read are 'ungot'. This means you can 'try' various patterns against the input 100% safely. This approach worked well for one parsing library I wrote. |
This comment has been minimized.
This comment has been minimized.
gsingh93
commented
Jan 20, 2015
|
Looks like there's a library that's doing this: https://github.com/mahkoh/scan Also, should this be closed or closed as postponed? |
nrc commentedMay 4, 2014
Add
scan!,scanln!, etc. macros which mirrorprint!,println!, etc. for doing straightforward input from stdin, a file, or other Reader.I really miss having something like this. I think it would be really good for increasing uptake of Rust to have a good story here.
I was hacking on a prototype implementation over the weekend. It only supports
{}holes (although some of the infrastructure for fixed widths is there) and doesn't work yet, but its getting there. I'd like to land something like that and then extend the mini-language gradually, letting the design evolve.