使用通用类型而不是提供的混凝土类型来关闭Rust中的特征,这可能吗?

发布于 2025-02-07 17:16:09 字数 1488 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2025-02-14 17:16:09

一如既往,生锈的编译器已经进行了营救……我对没有正确阅读错误的不好,简单的t和v并不符合IMP的范围,

简单地修复了错误:


struct BetterCacher<T,V>{
    calculation : T,
    value : HashMap<V,V>
}

impl<T,V> BetterCacher<T,V> //The first occurences of <T,V> brings it into scope
where
    T : Fn(V) -> V
{
    fn new(calculation : T) -> BetterCacher<T, V>{
        BetterCacher { calculation, value: HashMap::new() }
    }

    /*
     *fn get_or_generate_value(&mut self, arg : V) -> Option(V){
     *    let value_ref = self.value.get_mut()
     *}
     */
}

Well as always, the rust compiler has come to the rescue...my bad on not having read the error correctly, simply T and V were not in scope of impl,

Simply this fixed the error :


struct BetterCacher<T,V>{
    calculation : T,
    value : HashMap<V,V>
}

impl<T,V> BetterCacher<T,V> //The first occurences of <T,V> brings it into scope
where
    T : Fn(V) -> V
{
    fn new(calculation : T) -> BetterCacher<T, V>{
        BetterCacher { calculation, value: HashMap::new() }
    }

    /*
     *fn get_or_generate_value(&mut self, arg : V) -> Option(V){
     *    let value_ref = self.value.get_mut()
     *}
     */
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文