Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
827d1ea5
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
827d1ea5
编写于
3月 26, 2021
作者:
D
Dylan DPC
提交者:
GitHub
3月 26, 2021
浏览文件
操作
浏览文件
下载
差异文件
Rollup merge of #83456 - notriddle:vec-from-docs, r=JohnTitor
Add docs for Vec::from functions Part of #51430
上级
a054fd83
ef1bd577
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
61 addition
and
0 deletion
+61
-0
library/alloc/src/vec/mod.rs
library/alloc/src/vec/mod.rs
+61
-0
未找到文件。
library/alloc/src/vec/mod.rs
浏览文件 @
827d1ea5
...
...
@@ -2712,6 +2712,13 @@ fn as_mut(&mut self) -> &mut [T] {
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
impl
<
T
:
Clone
>
From
<&
[
T
]
>
for
Vec
<
T
>
{
/// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
///
/// # Examples
///
/// ```
/// assert_eq!(Vec::from(&[1, 2, 3][..]), vec![1, 2, 3]);
/// ```
#[cfg(not(test))]
fn
from
(
s
:
&
[
T
])
->
Vec
<
T
>
{
s
.to_vec
()
...
...
@@ -2724,6 +2731,13 @@ fn from(s: &[T]) -> Vec<T> {
#[stable(feature
=
"vec_from_mut"
,
since
=
"1.19.0"
)]
impl
<
T
:
Clone
>
From
<&
mut
[
T
]
>
for
Vec
<
T
>
{
/// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
///
/// # Examples
///
/// ```
/// assert_eq!(Vec::from(&mut [1, 2, 3][..]), vec![1, 2, 3]);
/// ```
#[cfg(not(test))]
fn
from
(
s
:
&
mut
[
T
])
->
Vec
<
T
>
{
s
.to_vec
()
...
...
@@ -2740,6 +2754,13 @@ fn from(s: &mut [T]) -> Vec<T> {
fn
from
(
s
:
[
T
;
N
])
->
Vec
<
T
>
{
<
[
T
]
>
::
into_vec
(
box
s
)
}
/// Allocate a `Vec<T>` and move `s`'s items into it.
///
/// # Examples
///
/// ```
/// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]);
/// ```
#[cfg(test)]
fn
from
(
s
:
[
T
;
N
])
->
Vec
<
T
>
{
crate
::
slice
::
into_vec
(
box
s
)
...
...
@@ -2751,6 +2772,20 @@ impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
where
[
T
]:
ToOwned
<
Owned
=
Vec
<
T
>>
,
{
/// Convert a clone-on-write slice into a vector.
///
/// If `s` already owns a `Vec<T>`, it will be returned directly.
/// If `s` is borrowing a slice, a new `Vec<T>` will be allocated and
/// filled by cloning `s`'s items into it.
///
/// # Examples
///
/// ```
/// # use std::borrow::Cow;
/// let o: Cow<[i32]> = Cow::Owned(vec![1, 2, 3]);
/// let b: Cow<[i32]> = Cow::Borrowed(&[1, 2, 3]);
/// assert_eq!(Vec::from(o), Vec::from(b));
/// ```
fn
from
(
s
:
Cow
<
'a
,
[
T
]
>
)
->
Vec
<
T
>
{
s
.into_owned
()
}
...
...
@@ -2760,6 +2795,15 @@ fn from(s: Cow<'a, [T]>) -> Vec<T> {
#[cfg(not(test))]
#[stable(feature
=
"vec_from_box"
,
since
=
"1.18.0"
)]
impl
<
T
,
A
:
Allocator
>
From
<
Box
<
[
T
],
A
>>
for
Vec
<
T
,
A
>
{
/// Convert a boxed slice into a vector by transferring ownership of
/// the existing heap allocation.
///
/// # Examples
///
/// ```
/// let b: Box<[i32]> = vec![1, 2, 3].into_boxed_slice();
/// assert_eq!(Vec::from(b), vec![1, 2, 3]);
/// ```
fn
from
(
s
:
Box
<
[
T
],
A
>
)
->
Self
{
let
len
=
s
.len
();
Self
{
buf
:
RawVec
::
from_box
(
s
),
len
}
...
...
@@ -2770,6 +2814,16 @@ fn from(s: Box<[T], A>) -> Self {
#[cfg(not(test))]
#[stable(feature
=
"box_from_vec"
,
since
=
"1.20.0"
)]
impl
<
T
,
A
:
Allocator
>
From
<
Vec
<
T
,
A
>>
for
Box
<
[
T
],
A
>
{
/// Convert a vector into a boxed slice.
///
/// If `v` has excess capacity, its items will be moved into a
/// newly-allocated buffer with exactly the right capacity.
///
/// # Examples
///
/// ```
/// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice());
/// ```
fn
from
(
v
:
Vec
<
T
,
A
>
)
->
Self
{
v
.into_boxed_slice
()
}
...
...
@@ -2777,6 +2831,13 @@ fn from(v: Vec<T, A>) -> Self {
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
impl
From
<&
str
>
for
Vec
<
u8
>
{
/// Allocate a `Vec<u8>` and fill it with a UTF-8 string.
///
/// # Examples
///
/// ```
/// assert_eq!(Vec::from("123"), vec![b'1', b'2', b'3']);
/// ```
fn
from
(
s
:
&
str
)
->
Vec
<
u8
>
{
From
::
from
(
s
.as_bytes
())
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录