rust - 抽象函数接收和返回 Rc
问题描述
我无法编译以下内容:
pub trait Symbol : ToString {
fn callee(self: &Rc<Self>) -> Option<Rc<dyn Symbol>> {
None
}
}
取Rc
参数会导致错误...
E0038)
symbols::Symbol
无法将特征制成对象
无处不Symbol
在,包括在该函数声明中。
如果我使用&self
而不是 ,它确实有效self: &Rc<Self>
,但是我有一些真正需要使用这个 Rc 的功能。知道该怎么做吗?
解决方案
请注意此错误消息:
error[E0307]: invalid `self` parameter type: Rc<(dyn Symbol + 'static)>
--> src/lib.rs:3:21
|
3 | fn callee(self: Rc<dyn Symbol>) -> Option<Rc<dyn Symbol>> {
| ^^^^^^^^^^^^^^
|
= note: type of `self` must be `Self` or a type that dereferences to it
= help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)
它应该是Rc<Self>
,它没有太大的使用意义,&Rc
因为Rc
克隆起来很便宜。
use std::rc::Rc;
pub trait Symbol : ToString {
fn callee(self: Rc<Self>) -> Option<Rc<dyn Symbol>> {
None
}
}
推荐阅读
- php - 使用 cakephp3 将多个值从表单发送到控制器
- java - JBOSS 服务器日志显示错误 SESSIONINFO 添加空值
- python - 创建 pyinstaller 可执行文件
- javascript - 使用对象 url 设置图像的 src 在电子中不起作用
- android - 如何在 AOSP 构建中将应用程序或 apk 安装到 data/app 目录中?
- postgresql - 服务器未监听所有 IP 地址
- c++ - 迭代器的问题
- docker - 如何将 Web 前端链接到后端
- python - 如何使用 Flask 和 OpenCV 显示图片服务器端?
- mongodb - mongodb中的spring boot store动态键值对列表