本文簡要介紹rust語言中 Function std::io::stdin_locked
的用法。
用法
pub fn stdin_locked() -> StdinLock<'static>
為當前進程的標準輸入構造一個新的鎖定句柄。
返回的每個句柄都是一個守衛,授予對共享全局緩衝區的鎖定訪問權限,該緩衝區的訪問通過互斥鎖進行同步。如果您需要對鎖定進行更明確的控製(例如,在多線程程序中),請使用 io::stdin
函數以及 Stdin::lock
方法來獲取未鎖定的句柄。
當返回的守衛超出範圍時,鎖被釋放。返回的守衛還實現了 Read
和 BufRead
特征以訪問基礎數據。
注意:該句柄鎖定的互斥體是不可重入的。即使在單線程程序中,調用其他訪問的代碼std::io::Stdin如果該鎖定句柄在該調用期間保持不變,則可能導致死鎖或Panics。
注意:Windows 可移植性考慮
在控製台中操作時,此流的 Windows 實現不支持非 UTF-8 字節序列。嘗試讀取無效的 UTF-8 字節將返回錯誤。
例子
#![feature(stdio_locked)]
use std::io::{self, BufRead};
fn main() -> io::Result<()> {
let mut buffer = String::new();
let mut handle = io::stdin_locked();
handle.read_line(&mut buffer)?;
Ok(())
}
相關用法
- Rust stdin用法及代碼示例
- Rust stderr_locked用法及代碼示例
- Rust stderr用法及代碼示例
- Rust stdout_locked用法及代碼示例
- Rust stdout用法及代碼示例
- Rust str.make_ascii_uppercase用法及代碼示例
- Rust str.strip_suffix用法及代碼示例
- Rust str.trim_left用法及代碼示例
- Rust str.char_indices用法及代碼示例
- Rust str.to_ascii_lowercase用法及代碼示例
- Rust str用法及代碼示例
- Rust str.trim用法及代碼示例
- Rust stringify用法及代碼示例
- Rust str.split_terminator用法及代碼示例
- Rust str.to_uppercase用法及代碼示例
- Rust str.starts_with用法及代碼示例
- Rust str.escape_default用法及代碼示例
- Rust str.rmatches用法及代碼示例
- Rust str.trim_right用法及代碼示例
- Rust str.rsplit_terminator用法及代碼示例
- Rust str.rsplit_once用法及代碼示例
- Rust str.split_once用法及代碼示例
- Rust str.len用法及代碼示例
- Rust str.chars用法及代碼示例
- Rust str.trim_left_matches用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Function std::io::stdin_locked。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。