Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

input - How does the Rust `String` type/`read_line` function know how much memory is needed without explicitly being told?

In C, before using the scanf or gets "stdio.h" functions to get and store user input, the programmer has to manually allocate memory for the data that's read to be stored in. In Rust, the std::io::Stdin.read_line function can seemingly be used without the programmer having to manually allocate memory prior. All it needs is for there to be a mutable String variable to store the data it reads in. How does it do this seemingly without knowledge about how much memory will be required?

question from:https://stackoverflow.com/questions/65921126/how-does-the-rust-string-type-read-line-function-know-how-much-memory-is-nee

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...