编译器允许我编写如下函数的全面实现:
trait Invoke {
type S;
type E;
fn fun(&mut self) -> Result<Self::S, Self::E>;
}
impl<F, S, E> Invoke for F
where
F: Fn() -> Result<S, E>,
{
type S = S;
type E = E;
fn fun(&mut self) -> Result<S, E> {
self()
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试添加函数参数时它开始抱怨:
trait Invoke {
type A;
type S;
type E;
fn fun(&mut self, arg: Self::A) -> Result<Self::S, Self::E>;
}
impl<F, A, S, E> Invoke for F
where
F: Fn(A) -> Result<S, E>,
{
type …Run Code Online (Sandbox Code Playgroud) 我有一个通用类型的特征.我想定义一个具有满足该特征的属性的结构,我想在该结构中实现一个使用特征内部函数的函数:
pub trait Point<I> {
fn id(&self) -> I;
}
pub struct Connection<T> {
pub to: T,
}
impl<T: Point> Connection<T> {
pub fn is_connected_to(&self, point: T) -> bool {
self.to.id() == point.id()
}
}
pub fn main() {
struct SimplePoint;
impl Point<char> for SimplePoint {
fn id(&self) -> char {
return 'A';
}
}
let a = SimplePoint {};
let conn = Connection { to: a };
}
Run Code Online (Sandbox Code Playgroud)
(游乐场)
如果我尝试运行此代码,则会收到错误消息:
error[E0243]: wrong number of type arguments: expected …Run Code Online (Sandbox Code Playgroud) rust ×2