When I was testing reading from standard input, I encountered a strange phenomenon. After I run the code and paste the following base64 text, the text cannot all be copied on the terminal and the keyboard keys fail. Only a fixed length of string can be copied on each run.
Whether running in an IDE or compiling and running via rustc, the phenomenon I described will occur. The only environment that can produce the expected running results is https://play.rust-lang.org/?version=stable&mode=debug&edition=2021 , sending text via the send button.
That sounds to me like some strange limitation of your local system rather than a problem with your program. What happens when you paste the string directly into the console or save it in a variable? Do you have another terminal you could try? Are you sure you have the complete string in your clipboard?
Thanks for your reply.
I can confirm that my clipboard has a complete base64 record. Because I tried to copy it directly in the terminal after closing the program. The copy function or the OS itself is normal.
Similarly, I performed all the steps on my Windows computer and was able to reproduce all the above problems 100%
The part you seem to be able to paste is exactly 1024 characters long. On macOS when the tty is in cooked mode (the default), the kernel will buffer up to 1024 bytes. Any character after that will be ignored until the input buffer is flushed by pressing newline. On Linux this limit is 4096 bytes and the rust playground likely uses a pipe rather than tty which doesn't have any limit at all. If you have the same problem on Windows, it may be the case that Windows also has a 1024 character limit.
The reason for this limit is because on a tty which is in cooked mode the kernel will buffer the input such that you can edit it before sending it back to the program by pressing enter. To avoid unbounded resource consumption, the kernel has a limit on the size of the buffer it uses to keep the input.
As workaround there are three options that I know of:
Have the user split the input in chunks smaller than 1024 characters (needs to be smaller to account for the newline character)
Have the user pipe in the string instead (echo '<long string>' | ./std_test or ./std_test < file_containing_large_string.txt)
Switch the terminal to raw mode and if you need the user to be able to press backspace and such, handle line editing yourself (eg in C you did likely use readline, while for Rust there are crates like rustyline) Make sure to switch the terminal back to cooked mode before exiting (even when you crash)! Otherwise the terminal will become effectively unusable to the user.
Thank you very much for your detailed description.
I just tried method 3 you mentioned, but it still doesn't work. After executing the following switch command, the same situation occurred as before changing the cooked mode.
stty raw
I tried method 2 you described before, and the result was completely correct, but I want to run and debug my code directly in the IDE, so I want to know if there are some adjustments that can make the console in my IDE behave the same as other programming languages.