- Start Date: 2015-1-30
- RFC PR: rust-lang/rfcs#771
- Rust Issue: rust-lang/rust#24443
Summary
Add a once
function to std::iter
to constructempty
function to construct
Motivation
This is a common task when working with iterators. Currently, this can be done in many ways, most of which are unergonomic, do not work for all types (e.g. requiring Copy/Clone), or both. once
and empty
are simple to implement,
Detailed design設計(する)
once
will return a new struct,std::iter::Once<T>
, implementingOnce<T>
is simply a newtype wrapper around std::option::IntoIter<T>
. The actualonce
is thus
empty
is similar:
These wrapper structs
Drawbacks
Although a tiny amount of code, it still does come with a testing, maintainance, etc. cost.
It's already possible to do this via Some(x).into_iter()
, std::iter::repeat(x).take(1)
(for x: Clone
), vec![x].into_iter()
, variousiterate
...
The existence of the Once
struct
Alternatives代わりのもの、選択肢
There are already many, many alternativesOption::into_iter()
, iterate
...
The Once
structstd::option::IntoIter
used instead.
Unresolved questions
Naturally, once
is fairly bikesheddable. one_time
? repeat_once
?
Are versions of once
that return &T
/&mut T
desirable?