Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
rust source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.84.0
rustc 1.85.0
rustc 1.86.0
rustc 1.87.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC assertions)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC assertions)
x86-64 GCCRS 14.2 (GCC)
x86-64 GCCRS 14.3 (GCC assertions)
x86-64 GCCRS 14.3 (GCC)
x86-64 GCCRS 15.1 (GCC assertions)
x86-64 GCCRS 15.1 (GCC)
Options
Source code
pub fn thing(vec: &mut Vec<Vec<u8>>, n: usize) { // Making the assembly smaller //if vec.len() != 0 || vec.capacity() != 2048 { // unsafe { // std::hint::unreachable_unchecked(); // } //} vec.resize(n, Vec::new()); }
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.84.0
rustc 1.85.0
rustc 1.86.0
rustc 1.87.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC assertions)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC assertions)
x86-64 GCCRS 14.2 (GCC)
x86-64 GCCRS 14.3 (GCC assertions)
x86-64 GCCRS 14.3 (GCC)
x86-64 GCCRS 15.1 (GCC assertions)
x86-64 GCCRS 15.1 (GCC)
Options
Source code
#![feature(trusted_len)] #![feature(iter_advance_by)] use std::fmt; use std::iter::{FusedIterator, TrustedLen}; use std::mem::{self, MaybeUninit}; use std::num::NonZero; /// Creates a new iterator that repeats a single element a given number of times. /// /// The `repeat_n()` function repeats a single value exactly `n` times. /// /// This is very similar to using [`repeat()`] with [`Iterator::take()`], /// but `repeat_n()` can return the original value, rather than always cloning. /// /// [`repeat()`]: crate::iter::repeat /// /// # Examples /// /// Basic usage: /// /// ``` /// use std::iter; /// /// // four of the number four: /// let mut four_fours = iter::repeat_n(4, 4); /// /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// /// // no more fours /// assert_eq!(None, four_fours.next()); /// ``` /// /// For non-`Copy` types, /// /// ``` /// use std::iter; /// /// let v: Vec<i32> = Vec::with_capacity(123); /// let mut it = iter::repeat_n(v, 5); /// /// for i in 0..4 { /// // It starts by cloning things /// let cloned = it.next().unwrap(); /// assert_eq!(cloned.len(), 0); /// assert_eq!(cloned.capacity(), 0); /// } /// /// // ... but the last item is the original one /// let last = it.next().unwrap(); /// assert_eq!(last.len(), 0); /// assert_eq!(last.capacity(), 123); /// /// // ... and now we're done /// assert_eq!(None, it.next()); /// ``` #[inline] fn repeat_n<T: Clone>(element: T, count: usize) -> RepeatN<T> { let element = if count == 0 { // `element` gets dropped eagerly. MaybeUninit::uninit() } else { MaybeUninit::new(element) }; RepeatN { element, count } } /// An iterator that repeats an element an exact number of times. /// /// This `struct` is created by the [`repeat_n()`] function. /// See its documentation for more. struct RepeatN<A> { count: usize, // Invariant: uninit iff count == 0. element: MaybeUninit<A>, } impl<A> RepeatN<A> { /// Returns the element if it hasn't been dropped already. fn element_ref(&self) -> Option<&A> { if self.count > 0 { // SAFETY: The count is non-zero, so it must be initialized. Some(unsafe { self.element.assume_init_ref() }) } else { None } } /// If we haven't already dropped the element, return it in an option. /// /// Clears the count so it won't be dropped again later. #[inline] fn take_element(&mut self) -> Option<A> { if self.count > 0 { self.count = 0; let element = mem::replace(&mut self.element, MaybeUninit::uninit()); // SAFETY: We just set count to zero so it won't be dropped again, // and it used to be non-zero so it hasn't already been dropped. unsafe { Some(element.assume_init()) } } else { None } } } impl<A: Clone> RepeatN<A> { #[inline] unsafe fn next_unchecked(&mut self) -> A { // SAFETY: The caller promised the iterator isn't empty self.count = unsafe { self.count.unchecked_sub(1) }; if self.count == 0 { // SAFETY: the check above ensured that the count used to be non-zero, // so element hasn't been dropped yet, and we just lowered the count to // zero so it won't be dropped later, and thus it's okay to take it here. unsafe { mem::replace(&mut self.element, MaybeUninit::uninit()).assume_init() } } else { // SAFETY: the count is non-zero, so it must have not been dropped yet. let element = unsafe { self.element.assume_init_ref() }; A::clone(element) } } } impl<A> Drop for RepeatN<A> { fn drop(&mut self) { self.take_element(); } } impl<A: Clone> Iterator for RepeatN<A> { type Item = A; #[inline] fn next(&mut self) -> Option<A> { if self.count > 0 { // SAFETY: Just checked it's not empty unsafe { Some(self.next_unchecked()) } } else { None } } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { let len = self.len(); (len, Some(len)) } #[inline] fn advance_by(&mut self, skip: usize) -> Result<(), NonZero<usize>> { let len = self.count; if skip >= len { self.take_element(); } if skip > len { // SAFETY: we just checked that the difference is positive Err(unsafe { NonZero::new_unchecked(skip - len) }) } else { self.count = len - skip; Ok(()) } } // trying to figure out if it helped /* #[inline] fn fold<B, F>(mut self, init: B, mut f: F) -> B where Self: Sized, F: FnMut(B, Self::Item) -> B, { if self.count == 0 { return init; } let element = unsafe { self.element.assume_init_ref() }; let init = (0..self.count.saturating_sub(1)).fold(init, |accum, _| { self.count = unsafe { self.count.unchecked_sub(1) }; f(accum, element.clone()) }); f(init, unsafe { self.take_element().unwrap_unchecked() }) } */ #[inline] fn last(mut self) -> Option<A> { self.take_element() } #[inline] fn count(self) -> usize { self.len() } } impl<A: Clone> ExactSizeIterator for RepeatN<A> { fn len(&self) -> usize { self.count } } impl<A: Clone> DoubleEndedIterator for RepeatN<A> { #[inline] fn next_back(&mut self) -> Option<A> { self.next() } #[inline] fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> { self.advance_by(n) } #[inline] fn nth_back(&mut self, n: usize) -> Option<A> { self.nth(n) } } impl<A: Clone> FusedIterator for RepeatN<A> {} unsafe impl<A: Clone> TrustedLen for RepeatN<A> {} pub fn thing(vec: &mut Vec<Vec<u8>>, n: usize) { // Making the assembly smaller //if vec.len() != 0 || vec.capacity() != 2048 { // unsafe { // std::hint::unreachable_unchecked(); // } //} vec.extend(repeat_n(Vec::new(), n)); }
rust source #3
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.84.0
rustc 1.85.0
rustc 1.86.0
rustc 1.87.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC assertions)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC assertions)
x86-64 GCCRS 14.2 (GCC)
x86-64 GCCRS 14.3 (GCC assertions)
x86-64 GCCRS 14.3 (GCC)
x86-64 GCCRS 15.1 (GCC assertions)
x86-64 GCCRS 15.1 (GCC)
Options
Source code
#![feature(trusted_len)] #![feature(iter_advance_by)] use core::fmt; use core::iter::{FusedIterator, TrustedLen}; use core::num::NonZero; /// Creates a new iterator that repeats a single element a given number of times. /// /// The `repeat_n()` function repeats a single value exactly `n` times. /// /// This is very similar to using [`repeat()`] with [`Iterator::take()`], /// but `repeat_n()` can return the original value, rather than always cloning. /// /// [`repeat()`]: crate::iter::repeat /// /// # Examples /// /// Basic usage: /// /// ``` /// use std::iter; /// /// // four of the number four: /// let mut four_fours = iter::repeat_n(4, 4); /// /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// assert_eq!(Some(4), four_fours.next()); /// /// // no more fours /// assert_eq!(None, four_fours.next()); /// ``` /// /// For non-`Copy` types, /// /// ``` /// use std::iter; /// /// let v: Vec<i32> = Vec::with_capacity(123); /// let mut it = iter::repeat_n(v, 5); /// /// for i in 0..4 { /// // It starts by cloning things /// let cloned = it.next().unwrap(); /// assert_eq!(cloned.len(), 0); /// assert_eq!(cloned.capacity(), 0); /// } /// /// // ... but the last item is the original one /// let last = it.next().unwrap(); /// assert_eq!(last.len(), 0); /// assert_eq!(last.capacity(), 123); /// /// // ... and now we're done /// assert_eq!(None, it.next()); /// ``` #[inline] pub fn repeat_n<T: Clone>(element: T, count: usize) -> RepeatN<T> { RepeatN { inner: RepeatNInner::new(element, count), } } #[derive(Clone)] struct RepeatNInner<T> { element: T, count: NonZero<usize>, } impl<T> RepeatNInner<T> { fn new(element: T, count: usize) -> Option<Self> { let count = NonZero::<usize>::new(count)?; Some(Self { element, count }) } } /// An iterator that repeats an element an exact number of times. /// /// This `struct` is created by the [`repeat_n()`] function. /// See its documentation for more. #[derive(Clone)] pub struct RepeatN<A> { inner: Option<RepeatNInner<A>>, } impl<A> RepeatN<A> { /// If we haven't already dropped the element, return it in an option. #[inline] fn take_element(&mut self) -> Option<A> { self.inner.take().map(|inner| inner.element) } } impl<A: fmt::Debug> fmt::Debug for RepeatN<A> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let (count, element) = match self.inner.as_ref() { Some(inner) => (inner.count.get(), Some(&inner.element)), None => (0, None), }; f.debug_struct("RepeatN") .field("count", &count) .field("element", &element) .finish() } } impl<A: Clone> Iterator for RepeatN<A> { type Item = A; #[inline] fn next(&mut self) -> Option<A> { let inner = self.inner.as_mut()?; let count = inner.count.get(); if let Some(decremented) = NonZero::<usize>::new(count - 1) { // Order of these is important for optimization let tmp = inner.element.clone(); inner.count = decremented; return Some(tmp); } return self.take_element(); } #[inline] fn size_hint(&self) -> (usize, Option<usize>) { let len = self.len(); (len, Some(len)) } #[inline] fn advance_by(&mut self, skip: usize) -> Result<(), NonZero<usize>> { let Some(inner) = self.inner.as_mut() else { return NonZero::<usize>::new(skip).map(Err).unwrap_or(Ok(())); }; let len = inner.count.get(); if let Some(new_len) = len.checked_sub(skip).and_then(NonZero::<usize>::new) { inner.count = new_len; return Ok(()); } self.inner = None; return NonZero::<usize>::new(skip - len).map(Err).unwrap_or(Ok(())); } #[inline] fn last(mut self) -> Option<A> { self.take_element() } #[inline] fn count(self) -> usize { self.len() } } impl<A: Clone> ExactSizeIterator for RepeatN<A> { fn len(&self) -> usize { self.inner .as_ref() .map(|inner| inner.count.get()) .unwrap_or(0) } } impl<A: Clone> DoubleEndedIterator for RepeatN<A> { #[inline] fn next_back(&mut self) -> Option<A> { self.next() } #[inline] fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> { self.advance_by(n) } #[inline] fn nth_back(&mut self, n: usize) -> Option<A> { self.nth(n) } } impl<A: Clone> FusedIterator for RepeatN<A> {} unsafe impl<A: Clone> TrustedLen for RepeatN<A> {} pub fn thing(vec: &mut Vec<Vec<u8>>, n: usize) { // Making the assembly smaller //if vec.len() != 0 || vec.capacity() != 2048 { // unsafe { // std::hint::unreachable_unchecked(); // } //} vec.extend(repeat_n(Vec::new(), n)); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree