相关疑难解决方法(0)

如何将字节向量(u8)转换为字符串

我想在Rust中编写简单的TCP/IP客户端,我需要打印出从服务器获得的缓冲区.如何将u8矢量转换String为打印?

rust

73
推荐指数
4
解决办法
5万
查看次数

将 actix_web 响应正文提取到字符串中的正确方法是什么?

我正在尝试使用actix_web来获取并显示网页的内容。HTTP 请求成功完成,我可以查看网页,但我想将正文读入String打印。

我尝试过let my_ip: String = response.body().into();,但收到一条错误消息

error[E0277]: the trait bound `std::string::String: std::convert::From<actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>>` is not satisfied
  --> src/main.rs:16:53
   |
16 |                 let my_ip: String = response.body().into();
   |                                                     ^^^^ the trait `std::convert::From<actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>>` is not implemented for `std::string::String`
   |
   = help: the following implementations were found:
             <std::string::String as std::convert::From<&'a str>>
             <std::string::String as std::convert::From<std::borrow::Cow<'a, str>>>
             <std::string::String as std::convert::From<std::boxed::Box<str>>>
             <std::string::String as std::convert::From<trust_dns_proto::error::ProtoError>>
   = note: required because of the requirements on the impl of `std::convert::Into<std::string::String>` for `actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>`
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所拥有的:

use …
Run Code Online (Sandbox Code Playgroud)

actor rust rust-actix

5
推荐指数
1
解决办法
4541
查看次数

如何为Hyper处理程序共享可变状态?

作为一个非常简单的例子,我正在尝试编写一个简单回复的网络服务器

此页面已被要求$ N次

但是我在分享可变状态方面遇到了很多麻烦.这是我最好的尝试:

extern crate hyper;

use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;

struct World {
    count: i64,
}

impl World {
    fn greet(&mut self, req: Request, res: Response) {
        self.count += 1;
        let str: String = format!("this page has been requested {} times", self.count);
        res.send(str.as_bytes()).unwrap();
    }
}

fn main() {
    println!("Started..");

    let mut w = World { count: 0 }; 

    Server::http("127.0.0.1:3001").unwrap()
        .handle(move |req: Request, res: Response| w.greet(req, res) ).unwrap();

}
Run Code Online (Sandbox Code Playgroud)

rust

4
推荐指数
1
解决办法
908
查看次数

如何对 futures::Stream::concat2 读取的字节数应用限制?

一个答案 如何阅读基于东京-超请求的整个身体?建议:

您可能希望对读取的字节数设置某种上限 [使用时futures::Stream::concat2]

我怎样才能真正做到这一点?例如,这里有一些代码模拟向我的服务发送无限量数据的恶意用户:

extern crate futures; // 0.1.25

use futures::{prelude::*, stream};

fn some_bytes() -> impl Stream<Item = Vec<u8>, Error = ()> {
    stream::repeat(b"0123456789ABCDEF".to_vec())
}

fn limited() -> impl Future<Item = Vec<u8>, Error = ()> {
    some_bytes().concat2()
}

fn main() {
    let v = limited().wait().unwrap();
    println!("{}", v.len());
}
Run Code Online (Sandbox Code Playgroud)

future rust

2
推荐指数
1
解决办法
233
查看次数

标签 统计

rust ×4

actor ×1

future ×1

rust-actix ×1