-1

我是一个生锈的新手,在运行一些简单的 ws 示例时遇到了很多麻烦。

我在 Cargo.toml 文件中有一个新项目:

[package]
name = "ouranos4"
version = "0.1.0"
authors = ["jasongoodwin <jay.michael.goodwin@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1", features = ["full"] }
warp = "0.3"

然后我在这里找到了 warp ws 示例:https ://github.com/seanmonstar/warp/blob/master/examples/websockets.rs

// use futures::{FutureExt, StreamExt};
use warp::Filter;

#[tokio::main]
async fn main() {
    let routes = warp::path("echo")
        // The `ws()` filter will prepare the Websocket handshake.
        .and(warp::ws())
        .map(|ws: warp::ws::Ws| {
            // And then our closure will be called when it completes...
            ws.on_upgrade(|websocket| {
                // Just echo all messages back...
                let (tx, rx) = websocket.split();
                rx.forward(tx).map(|result| {
                    if let Err(e) = result {
                        eprintln!("websocket error: {:?}", e);
                    }
                })
            })
        });

    warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}

我发誓这是在编译,但出乎意料的是我得到了这些错误:

   Compiling ouranos4 v0.1.0 (/Users/jasongoodwin/Development/src/ouranos4)
error[E0599]: no method named `split` found for struct `WebSocket` in the current scope
    --> src/main.rs:13:42
     |
13   |                 let (tx, rx) = websocket.split();
     |                                          ^^^^^ method not found in `WebSocket`
     |
    ::: /Users/jasongoodwin/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.14/src/stream/stream/mod.rs:1359:8
     |
1359 |     fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)
     |        ----- the method is available for `Box<WebSocket>` here
     |
     = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
     |
2    | use futures_util::stream::stream::StreamExt;
     |

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: could not compile `ouranos4`

我重新创建了项目,清理了项目,做了所有可以想象的事情。将 rust 编译器版本切换为 nightly 并返回。我真的很困惑为什么我现在不能拆分这个。我试过不同的例子,不同的箱子,我一直看到这个错误。我很想扔掉我的笔记本电脑并在另一台计算机上尝试:P

4

1 回答 1

0

我需要添加更多的进口!感谢kmdreko。

use futures::StreamExt;
use futures::FutureExt;
于 2021-04-30T21:43:23.140 回答