小编Rij*_*kii的帖子

没有为 AsRef<Path> 找到名为 `join` 的方法

我有一个函数,它AsRef<Path>作为一个参数,看起来像这样

fn test<P: AsRef<std::path::Path>>(path: P) {
    path.join("13123123");
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,它给了我以下错误

fn test<P: AsRef<std::path::Path>>(path: P) {
    path.join("13123123");
}
Run Code Online (Sandbox Code Playgroud)

rust

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

如何实现轮询异步 fn 的 Future 或 Stream?

我有一个Test我想实现的结构体std::future::Future,它会轮询function

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

struct Test;

impl Test {
    async fn function(&mut self) {}
}

impl Future for Test {
    type Output = ();
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.function() {
            Poll::Pending => Poll::Pending,
            Poll::Ready(_) => Poll::Ready(()),
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

那没有用:

error[E0308]: mismatched types
  --> src/lib.rs:17:13
   |
10 |     async fn function(&mut self) {}
   |                                  - the `Output` of this `async fn`'s expected opaque …
Run Code Online (Sandbox Code Playgroud)

asynchronous unsafe future rust async-await

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

如何在另一个宏中定义一个具有无限量参数的宏?

这有效,但test_macro只接受一个参数:

macro_rules! test_define (
    ($name:ident) => (
        macro_rules! $name (
            ( $x:expr ) => (
                // something
            )
        );
    )
);

test_define!(test_macro);
Run Code Online (Sandbox Code Playgroud)

如果我尝试这样做:

macro_rules! test_define2 (
    ($name:ident) => (
        macro_rules! $name (
            ( $($x:expr),* ) => (
                // something
            )
        );
    )
);

test_define2!(test_macro2);
Run Code Online (Sandbox Code Playgroud)

编译失败:

error: attempted to repeat an expression containing no syntax variables matched as repeating at this depth
 --> src/main.rs:4:16
  |
4 |             ( $($x:expr),* ) => (
  |                ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

rust

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

标签 统计

rust ×3

async-await ×1

asynchronous ×1

future ×1

unsafe ×1