为什么impl线和new_in线的where线都是A: allocator?在文档中对单个方法可见trait绑定是否只是为了文档,或者如果省略where绑定,含义会有所不同?

在这段摘自https://doc.rust-lang.org/src/alloc/boxed.rs.html#378-380

impl<T, A: Allocator> Box<T, A> {
    /// Allocates memory in the given allocator then places `x` into it.
    ///
    /// This doesn't actually allocate if `T` is zero-sized.
    ///
    /// # Examples
    ///
    /// ```
    /// #![feature(allocator_api)]
    ///
    /// use std::alloc::System;
    ///
    /// let five = Box::new_in(5, System);
    /// ```
    #[cfg(not(no_global_oom_handling))]
    #[unstable(feature = "allocator_api", issue = "32838")]
    #[must_use]
    #[inline]
    pub fn new_in(x: T, alloc: A) -> Self
    where
        A: Allocator,
    {
        let mut boxed = Self::new_uninit_in(alloc);
        unsafe {
            boxed.as_mut_ptr().write(x);
            boxed.assume_init()
        }
    }

推荐答案

这似乎是一个遗留下来的问题.

Originally, the bounds were only on the impl.但是,在PR #91884中,new_inconst化(有条件地,取决于分配器的const度),因此Impl上的A: Allocator不再足够-它需要变成A: ~const Allocator(一个可能的-const界限),这只能放在方法上.但是后来,关于常量特征功能的设计问题被提出,导致它在STD中的使用被删除,包括PR #Box693中的Box.在该公关中,只删除了~const,边界保持不变,留下了多余的边界.

Rust相关问答推荐

包含嵌套 struct 的CSV

当两者都有效时,为什么Rust编译器建议添加';&;而不是';*';?

如果成员都实现特征,是否在多态集合上实现部分重叠的特征?

Box::new()会从一个堆栈复制到另一个堆吗?

默认特征实现中的生命周期问题

在使用#[NO_STD]时,如何在Rust中收到紧急消息?

如何删除Mac Tauri上的停靠图标?

为昂贵的for循环制作筛子

使用Py03从Rust调用Python函数时的最佳返回类型

通过RabbitMQ取消铁 rust 中长时间运行的人造丝任务的策略

为什么Option类型try块需要类型注释?

为什么RefCell没有与常规引用相同的作用域?

为什么 Rust 创建的 f32 小于 f32::MIN_POSITIVE?

push 方法是否取得所有权?

使用 rust 在 google cloud run (docker) 中访问环境变量的适当方法

无法把握借来的价值不够长寿,请解释

为什么 no_std crate 可以依赖于使用 std 的 crate?

将文件的第一行分别读取到文件的其余部分的最有效方法是什么?

隐式类型闭包的错误生命周期推断

为什么 std::iter::Peekable::peek 可变地borrow self 参数?