我試圖使用Rust和Iron實現教育客戶端 - 服務器應用程序。我遇到了我無法理解的行爲。下面是代碼:Iron :: new():: http()攔截stdin
fn main() {
Iron::new(hello_world).http("localhost:3000").unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read line");
println!("You entered: {}", &input)
}
fn hello_world(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "Hello World!")))
}
當我運行它,並嘗試從鍵盤輸入的東西,行您輸入:某些文本沒有出現。
但之後我改變了這一行:
Iron::new(hello_world).http("localhost:3000").unwrap();
有了這個:
let listener = Iron::new(hello_world).http("localhost:3000").unwrap();
你進入我得到的字符串:我的控制檯上的一些文字。所以它似乎工作。但是現在我有關於未使用的變量的警告。這種行爲令人困惑。
任何人都可以解釋爲什麼這實際上發生?
因爲當你把聽者它會阻止:
引入變量的結果直到服務器死亡,當你將它移動到一個變量時,它會在變量被丟棄時(即作用域結束)阻塞 - 你可以用'_'(l ike'_listener')沉默未使用的警告。 –
謝謝。並感謝'_listener'的精彩入侵。我不知道這件事。 –