-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add std::io::input
simple input function.
#74178
Conversation
Condenses the normal input process into one function. This is good for new rust users who don't understand the normal process and for users who don't require special functionality.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I would suggest having two set of API, pub fn inputln(prompt: &str) -> String {}
pub fn input() -> String {} I could think of a use like let name = inputln("name: ");
let age = input(); I also don't know if it will be good to have I wonder if these kind of pull request requires a FCP first. Sidenote: @sHaDoW-54 can you please format the code with rustfmt and use ```rust? |
- Properly formated. - Added ```rust for docs. - Split function into two : ```input()``` and ```inputln(&str)```. - Deleted unnecessary comment for non-windows users. - Removed ```print!()``` macro. - Added warning not recommending this method for normal use.
- Accounted for all return options, ("\n"), ("\r"), ("\r\n"), ("\n\r")
Streamlined the ```inputln(&str)``` function.
Changed ```inputln(&str)``` function name to better represent the function.
Fixed docs
I think this would be a very useful addition to |
Attempting to fix, Error unused function.
Set functions as unstable
Use input functions.
Flagged by tidy for trailing whitespace?
Co-authored-by: Ivan Tham <pickfire@riseup.net>
Co-authored-by: Ivan Tham <pickfire@riseup.net>
Co-authored-by: Ivan Tham <pickfire@riseup.net>
doc fix
Given these functions would start out as unstable, and it really is just one or two functions, at most we'd want a libs team FCP. A full RFC is overkill. If someone wants to come up with a more comprehensive solution in the meantime, by all means, but that shouldn't block the unstable addition of these simple functions. I regularly write simple Rust scripts where I'd love to quickly read some input but it's always such a hassle to pull out the stdio machinery to do it so these functions are perfect for me. |
Seconded. There's essentially no performance benefit for having a no-output version. An empty string is a constant and the performance is limited by the human anyway.
How about |
Merged both functions
remove excess code
removed unused feature
remove double locking Co-authored-by: Joshua Nelson <joshua@yottadb.com>
docs fix
docs fix
lock fix
fix double lock, again
Oh, why merge prompt and input? I think it should be separated. Why print an empty string when you can not print at all? It looks like an extra useless step is being called when user have |
Co-authored-by: Ivan Tham <pickfire@riseup.net>
@pickfire Do you mean something like the difference between |
@sHaDoW-54 I meant something like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some suggestions to move the code examples in line with the standard style, that is, import types, but don't import functions directly.
One thing to consider about adding functionality like this is its impact on docs. Consider https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html, which is where it would be used in the book; this function would decently simplify the very beginning.
docs fix
docs stuff
☔ The latest upstream changes (presumably #73265) made this pull request unmergeable. Please resolve the merge conflicts. |
@sHaDoW-54 |
reworked in #75435 |
I wrote the design in https://hackmd.io/@G8ZK5BSuQPOxvEQWVZSxng/By2UUacFD/edit, hopefully it covers the design space. |
Condenses the normal input process into one function. This is good for new rust users who don't understand the normal process and for users who don't require special functionality.
Especially with new rust users looking to make small programs, input can be a tricky thing for them to grasp, especially when other languages do this much simpler.
EX:
Python :
user_input = input("Enter: ")
Ruby:
user_input = gets
C#:
user_input = Console.ReadLine();
So this...
Would turn into this...