Skip to content

alloc: Unstably add Vec::from_elem and Vec::from_elem_in - #164043

Open
Gelbpunkt wants to merge 1 commit into
rust-lang:mainfrom
Gelbpunkt:from-elem-in
Open

Gelbpunkt wants to merge 1 commit into
rust-lang:mainfrom
Gelbpunkt:from-elem-in

Conversation

@Gelbpunkt

Copy link
Copy Markdown
Contributor

These methods are of particular interest for users because they perform better due to specialization, e.g. when elem is zero. In that case, it will result in a call to calloc or equivalent, which is better than a call to malloc followed by memset.

We also forward the existing std::vec::from_elem and std::vec::from_elem_in to these new associated methods on Vec. The module-level methods could potentially be removed in a follow-up.

Zulip thread

r? @clarfonthey

These methods are of particular interest for users because they perform
better due to specialization, e.g. when `elem` is zero. In that case, it
will result in a call to `calloc` or equivalent, which is better than a
call to `malloc` followed by `memset`.

We also forward the existing `std::vec::from_elem` and
`std::vec::from_elem_in` to these new associated methods on `Vec`. The
module-level methods could potentially be removed in a follow-up.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 9, 2026
/// assert_eq!(vec, [0; 3]);
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "vec_from_elem", issue = "none")]

@Gelbpunkt Gelbpunkt Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part of an existing feature or should we create a new tracking issue for this? Also, should from_elem_in use the same feature or does it belong in allocator_ext?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put both of these into their own from_elem issue since I have a feeling allocator_ext will get stabilised first. But yes, should have one.

@folkertdev folkertdev added the F-abi_custom `#![feature(abi_custom)]` label Oct 9, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-aarch64-gnu-llvm-22-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
To only update this specific test, also pass `--test-args suggestions/deref-path-method.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/suggestions/deref-path-method.rs" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/suggestions/deref-path-method" "-Znext-solver=coherence" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "--edition=2015"
stdout: none
--- stderr -------------------------------
error[E0599]: no associated function or constant named `contains` found for struct `Vec<_, _>` in the current scope
##[error]  --> /checkout/tests/ui/suggestions/deref-path-method.rs:3:10
   |
---
      and 9 others
  --> /rustc/FAKE_PREFIX/library/alloc/src/vec/mod.rs:464:4
help: the function `contains` is implemented on `[_]`
   |
LL -     Vec::contains(&vec, &0);
LL +     <[_]>::contains(&vec, &0);
   |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.

@folkertdev folkertdev added A-allocators Area: Custom and system allocators and removed F-abi_custom `#![feature(abi_custom)]` labels Oct 9, 2026
#[doc(hidden)]
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {

@clarfonthey clarfonthey Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just remove this since it's unstable and make the macro call the associated methods in both cases. The only reason the standalone from_elem shouldn't just be removed also is because it's currently marked as stable, and we want to see how #164053 goes.

View changes since the review

@clarfonthey

Copy link
Copy Markdown
Contributor

r? ChrisDenton

Offloading a few PRs, Chris should have enough context to handle this.

@rustbot rustbot assigned ChrisDenton and unassigned clarfonthey Oct 11, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-allocators Area: Custom and system allocators S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants