小编Joh*_*ove的帖子

如何编写一个知道实现者是 [u8] 的特征方法?

我正在实现一个特征&[u8],但无法self在特征实现中使用。我假设该特征无法检测类型,我应该使用一个where子句,但我不知道如何在没有实现者的情况下使用它。

use std::fmt::Debug;

pub trait Xor: Debug {
    fn xor(&self, key_bytes: &[u8]) -> &[u8] {
        for n in &self[..] {
            dbg!(n);
        }
        unimplemented!()
    }
}

impl Xor for [u8] {}

fn main() {
    let xa = b"1234";
    xa.xor(b"123");
}
Run Code Online (Sandbox Code Playgroud)

操场

use std::fmt::Debug;

pub trait Xor: Debug {
    fn xor(&self, key_bytes: &[u8]) -> &[u8] {
        for n in &self[..] {
            dbg!(n);
        }
        unimplemented!()
    }
}

impl Xor for [u8] {}

fn main() {
    let xa …
Run Code Online (Sandbox Code Playgroud)

rust

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

如何创建嵌套结构?

我正在实现一个具有嵌套结构调用的方法。我读到有关 Rust 生命周期的内容,我认为我的问题是关于生命周期的,但我无法理解如何在代码中使用它。

#[derive(Debug)]
pub struct Request {
    Header: String
} 
#[derive(Debug)]
pub enum Proto {
    HTTP,
    HTTPS
}

#[derive(Debug)]
pub struct HTTP {
    ssss: Request,
    names: Proto,
}

impl HTTP {
    pub fn new(name: Proto) -> HTTP {
        HTTP{
            ssss.Header: "Herman".to_string(),
            names: name,
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不可能给 赋值ssss.Header

#[derive(Debug)]
pub struct Request {
    Header: String
} 
#[derive(Debug)]
pub enum Proto {
    HTTP,
    HTTPS
}

#[derive(Debug)]
pub struct HTTP {
    ssss: Request,
    names: Proto,
}

impl HTTP { …
Run Code Online (Sandbox Code Playgroud)

struct rust

0
推荐指数
1
解决办法
2637
查看次数

在Hyper 0.11中没有为类型`hyper :: Client`找到名为`post`的方法

我想用Hyper来制作HTTP请求.呼唤Client::get精细的作品,但其他方法如Client::postClient::head引起编译错误.

extern crate futures;
extern crate hyper;
extern crate tokio_core;

use std::io::{self, Write};
use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Core;

fn main() {
    let mut core = Core::new().unwrap();
    let client = Client::new(&core.handle());

    let uri = "http://httpbin.org/ip".parse().unwrap();
    let work = client.post(uri).and_then(|res| {
        // if post changed to get it will work correctly
        println!("Response: {}", res.status());

        res.body("x=z")
            .for_each(|chunk| io::stdout().write_all(&chunk).map_err(From::from))
    });
    core.run(work).unwrap();
}
Run Code Online (Sandbox Code Playgroud)

错误:

error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope …
Run Code Online (Sandbox Code Playgroud)

rust hyper

0
推荐指数
1
解决办法
624
查看次数

标签 统计

rust ×3

hyper ×1

struct ×1