我有一个通用类型的特征.我想定义一个具有满足该特征的属性的结构,我想在该结构中实现一个使用特征内部函数的函数:
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) 如果我定义这样的函数:
const getName = async () => await Promise.resolve('John');
Run Code Online (Sandbox Code Playgroud)
当我尝试使用异步调用getName函数时:
const name = await getName();
console.log(`Hi ${name}`);
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误:
const name = await getName();
^^^^^
SyntaxError: await is only valid in async function
Run Code Online (Sandbox Code Playgroud)
我做错了什么?