Well, if you want a detailed explanation, you can dig a bit into the read_line
method which is part of the BufRead
trait. Heavily simplified, the function look like this.
fn read_line(&mut self, target: &mut String)
loop {
// That method fills the internal buffer of the reader (here stdin)
// and returns a slice reference to whatever part of the buffer was filled.
// That buffer is actually what you need to allocate in advance in C.
let available = self.fill_buf();
match memchr(b'
', available) {
Some(i) => {
// A '
' was found, we can extend the string and return.
target.push_str(&available[..=i]);
return;
}
None => {
// No '
' found, we just have to extend the string.
target.push_str(available);
},
}
}
}
So basically, that method extends the string as long as it does not find a
character in stdin
.
If you want to allocate a bit of memory in advance for the String
that you pass to read_line
, you can create it using String::with_capacity
. This will not prevent the String
to reallocate if it is not large enough though.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…