相关疑难解决方法(0)

如何直接创建String?

.to_string()当我需要一个字符串时,有什么方法可以避免调用吗?例如:

fn func1(aaa: String) -> ....
Run Code Online (Sandbox Code Playgroud)

而不是

func1("fdsfdsfd".to_string())
Run Code Online (Sandbox Code Playgroud)

我可以这样做:

func1(s"fdsfdsfd")
Run Code Online (Sandbox Code Playgroud)

rust

10
推荐指数
3
解决办法
709
查看次数

是否可以将字节解码为UTF-8,将错误转换为Rust中的转义序列?

在Rust中,通过这样做可以从字节中获取UTF-8:

if let Ok(s) = str::from_utf8(some_u8_slice) {
    println!("example {}", s);
}
Run Code Online (Sandbox Code Playgroud)

这可能有效,也可能没有,但Python有能力处理错误,例如:

s = some_bytes.decode(encoding='utf-8', errors='surrogateescape');
Run Code Online (Sandbox Code Playgroud)

在此示例中,参数surrogateescape将无效的utf-8序列转换为转义码,因此它们不是忽略或替换无法解码的文本,而是替换为有效的字节文字表达式utf-8.请参阅:Python文档了解详细信息.

Rust是否有办法从字节中获取UTF-8字符串,从而逃避错误而不是完全失败?

utf-8 rust unicode-escapes

6
推荐指数
1
解决办法
1765
查看次数

将 &[u8] 转换为字符串

我有一个字节数组,我想将其返回为std::string::String. 我发现的其他答案和文档是将 Vectors 转换为字符串。

我如何将字节数组转换&[u8]为 a String

types rust

6
推荐指数
1
解决办法
5141
查看次数

将 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
查看次数

当我无法更改打印的代码时,如何捕获 stdout/stderr 的内容?

foo我有一个无法修改的函数,并且包含其中的代码println!eprintln!

fn foo() {
    println!("hello");
}
Run Code Online (Sandbox Code Playgroud)

调用该函数后,我必须测试它打印的内容,因此我想将 stdout/stderr 捕获到变量中。

rust

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

如何阅读基于Tokio的Hyper请求的整个主体?

我想使用Hyper的当前主分支编写服务器,该分支保存由POST请求传递的消息,并将此消息发送到每个传入的GET请求.

我有这个,大多是从Hyper示例目录中复制的:

extern crate futures;
extern crate hyper;
extern crate pretty_env_logger;

use futures::future::FutureResult;

use hyper::{Get, Post, StatusCode};
use hyper::header::{ContentLength};
use hyper::server::{Http, Service, Request, Response};
use futures::Stream;

struct Echo {
    data: Vec<u8>,
}

impl Echo {
    fn new() -> Self {
        Echo {
            data: "text".into(),
        }
    }
}

impl Service for Echo {
    type Request = Request;
    type Response = Response;
    type Error = hyper::Error;
    type Future = FutureResult<Response, hyper::Error>;

    fn call(&self, req: Self::Request) -> Self::Future {
        let resp = match …
Run Code Online (Sandbox Code Playgroud)

rust hyper rust-tokio

3
推荐指数
2
解决办法
2740
查看次数

如何将 bytes::Bytes 转换为 &amp;str 而不进行任何复制?

我有一个bytes::Bytes(在Actix的的web请求在这种情况下,它的身体),并且需要一个字符串参数切片另一个函数:foo: &str。将 转换bytes::Bytes&str以便不制作副本的正确方法是什么?我试过了,&body.into()但我得到:

the trait `std::convert::From<bytes::bytes::Bytes>` is not implemented for `str`
Run Code Online (Sandbox Code Playgroud)

以下是基本的函数签名:

pub fn parse_body(data: &str) -> Option<&str> {
    // Do stuff
    // ....
    Ok("xyz")
}

fn consume_data(req: HttpRequest<AppState>, body: bytes::Bytes) -> HttpResponse {
    let foo = parse_body(&body);
    // Do stuff
    HttpResponse::Ok().into()
}
Run Code Online (Sandbox Code Playgroud)

type-conversion binary-data rust

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