当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust Box.try_new_in用法及代码示例


本文简要介绍rust语言中 std::boxed::Box.try_new_in 的用法。

用法

pub fn try_new_in(x: T, alloc: A) -> Result<Box<T, A>, AllocError>

在给定的分配器中分配内存,然后将 x 放入其中,如果分配失败则返回错误

如果 T 大小为零,则实际上不会分配。

例子

#![feature(allocator_api)]

use std::alloc::System;

let five = Box::try_new_in(5, System)?;

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 std::boxed::Box.try_new_in。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。