锈,将src拆分为多个文件

时间:2018-11-18 10:38:20

标签: syntax rust rust-tokio

我得到了以下代码:(来自rust-tokio的示例) src / main.rs ::

extern crate futures;
extern crate tokio;
mod hw;

use hw::{Display, HelloWorld};

fn main() {
    let future = Display(HelloWorld);
    tokio::run(future);
}

和src / hw.rs ::

extern crate futures;
extern crate tokio;

pub use futures::{Async, Future, Poll, try_ready};
pub use std::fmt;

pub struct HelloWorld;
impl Future for HelloWorld {
    type Item = String;
    type Error = ();

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        Ok(Async::Ready("helloworld".to_string()))
    }
}

pub struct Display<T>(T);
impl<T> Future for Display<T>
where
    T: Future,
    T::Item: fmt::Display,
{
    type Item = ();
    type Error = T::Error;

    fn poll(&mut self) -> Poll<(), T::Error> {
        let value = try_ready!(self.0.poll());
        println!("{}", value);
        Ok(Async::Ready(()))
    }
}

,出现错误:

error[E0423]: expected function, found struct `Display`
--> src/main.rs:12:18                                                                                                                                                                            
   |                                                                                                                                                                                               
12 |     let future = Display(HelloWorld);                                                                                                                                                         
   |                  ^^^^^^^ constructor is not visible here due to private   fields                                                                                                                

但是,如果我拆分这些文件并删除任何“ mod”和“ use”,一切都很好。 那么,“正确”错误是什么:“函数vs结构”或“构造函数不可见”? 请帮助

0 个答案:

没有答案