-
Notifications
You must be signed in to change notification settings - Fork 18
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
Request: ability to read a line into a passed &mut String
#59
Comments
I understand that this is the convention applied to line-reading operations on The primary reason being that the result of |
Right, I'm not suggesting that you alter the existing Instead, I'd like a separate method that can exist in parallel. Something like this pub fn read_to_string(&self, &mut String) -> Result<ReadToResult> { ... }
pub enum ReadToResult {
Eof,
// the data is already in the String, so we just have an empty variant here
Success,
Signal(Signal),
} |
Whether it's a change to |
Sure, that's a general danger with methods that allow people to provide a buffer, they might ignore the output. However, since we specifically have a warning against that in Rust ("warning: unused must_use"), I think it's reasonable to allow people to use that style API if they want. Basically, the library should offer a path where the library user allocates the memory and the library just does stuff with the memory given. |
@murarth I'd like to point you at the
That's all reading functions. There is no function in
… is either not misleading at all, or that whoever might be mislead by that is also not a user of I/O in Rust and therefore wouldn't use this crate. |
@Evrey: I am quite familiar with the In my most recent comment, I made the point that this similarity is itself a possible source of confusion. Allow me to demonstrate what I mean by that. This is a valid and correct usage of let mut buf = String::new();
loop {
stdin().read_line(&mut buf)?;
if buf.is_empty() {
break;
}
process_input(&buf);
buf.clear();
} If one were to naively substitute |
For the version where you read into a buffer, you can move the Signal and EOF outputs into the Err side of the result, and then when they happen the user won't think that things are 100% fine. They'll inspect the error and then see what's up. |
Using the |
So, is there any way for there to be a low-allocation-chance version of reading in a line to an existing String? (I understand that of course if the input is very long it would realloc the String as it pushes in characters). |
I'd like the ability to provide a
&mut String
value to something similar to Interface::read_line and then have it write the line message into there, instead of allocating a newString
each time.The text was updated successfully, but these errors were encountered: