From a74e1686d979fbf5688de549a9b2a951b1fb278f Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Wed, 25 Apr 2018 15:00:59 -0700 Subject: [PATCH] Add implementation of Extend for () --- src/libcore/iter/traits.rs | 7 +++++++ src/test/run-pass/extend-for-unit.rs | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/test/run-pass/extend-for-unit.rs diff --git a/src/libcore/iter/traits.rs b/src/libcore/iter/traits.rs index ddbb5998942..b9987212070 100644 --- a/src/libcore/iter/traits.rs +++ b/src/libcore/iter/traits.rs @@ -352,6 +352,13 @@ pub trait Extend { fn extend>(&mut self, iter: T); } +#[stable(feature = "extend_for_unit", since = "1.28.0")] +impl Extend<()> for () { + fn extend>(&mut self, iter: T) { + iter.into_iter().for_each(drop) + } +} + /// An iterator able to yield elements from both ends. /// /// Something that implements `DoubleEndedIterator` has one extra capability diff --git a/src/test/run-pass/extend-for-unit.rs b/src/test/run-pass/extend-for-unit.rs new file mode 100644 index 00000000000..d1a0614bd8a --- /dev/null +++ b/src/test/run-pass/extend-for-unit.rs @@ -0,0 +1,20 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub fn main() { + let mut x = 0; + { + let iter = (0..5).map(|_| { + x += 1; + }); + ().extend(iter); + } + assert_eq!(x, 5); +}