我正在尝试在Rust中编写一个echo服务器.
use std::net::{TcpStream, TcpListener};
use std::io::prelude::*;
fn main() {
let listener = TcpListener::bind("0.0.0.0:8000").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
println!("A connection established");
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
stream.write(&buffer[..]).unwrap();
stream.flush().unwrap();
}
Run Code Online (Sandbox Code Playgroud)
第一个请求nc localhost 8000正在按预期工作,但后续请求不是.我究竟做错了什么?服务器如何读取客户端请求的问题是什么?虽然服务器端没有错误.
我通过在终端上输入数据来发送数据:
$ nc localhost 8000
hi
hi
hello
# no response
# on pressing enter
Ncat: Broken pipe.
Run Code Online (Sandbox Code Playgroud) Go 通道可用于在 goroutine 之间进行通信。类似地,yield 和 next 语法可用于在两个生成器之间进行通信。我的假设正确吗?如果不是,我哪里错了?
我正在尝试实现greps项目,我陷入了搜索功能.
fn search<'a, T>(query: &T, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(query) {
results.push(line);
}
}
results
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
rustc 1.18.0 (03fc9d622 2017-06-06)
error[E0277]: the trait bound `T: std::ops::Fn<(char,)>` is not satisfied
--> <anon>:39:17
|
39 | if line.contains(query) {
| ^^^^^^^^ the trait `std::ops::Fn<(char,)>` is not implemented for `T`
|
= help: consider adding a `where T: std::ops::Fn<(char,)>` bound
= note: required because of the requirements on …Run Code Online (Sandbox Code Playgroud)