You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for Vec::resize_with and resize_default #41758
A-slice-vecArea: `[T]` and `Vec<T>`B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs-SmallLibs issues that are considered "small" or self-containedT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Currently, Vec has a resize method which takes a new parameter so that if the resize involves growing the vector, the new parameter is cloned into each newly-created cell in the vector. T must implement Clone in order for this method to be available.
It would be useful to add a resize_default method that instead requires that T: Default, and calls T::default() to fill the newly-created cells. Not only would this be ergonomic, but for certain implementations of T::default, it might allow the compiler to make better optimizations.
@rust-lang/libs Nominating for stabilization. The only potential concern is that we can generalize this to take a closure (see below). I don't know that it'd be all that useful, though, so I'm inclined to say that we shouldn't do so, and Clone and Default are the two "producing" traits in std, so standardizing around them seems logical.
fnresize_with(&mutself,new_len:usize,f:F)whereF:FnMut() -> T
@Mark-Simulacrum I'd be willing to make a PR that adds resize_with under a separate feature flag. I think that all three are useful; resize_default would simply be documented as equivalent to resize_with(Default::default)
I might lean more towards just having resize_with, for now at least. It's strictly more powerful and doesn't seem all that more verbose than resize_default?
For completeness I've scanned crates.io for uses of .resize_with, exact command used was rg --iglob '*.rs' -F '.resize_with'. Here are the results: https://pastebin.com/UgpzzQxT
At a glance the results seem to align with the ones posted above - roughly half of the invocations just use the default value. On the other hand, there are just 207 uses of resize_with across the entire ecosystem, so I'm not sure adding another method for a relatively niche use case is worth it.
for reference, how many unwrap_or with the default value for the type (0, "", etc)? how many unwrap_or_else with Default::default? is it possible that the problem is elsewhere? (bad docs, bad book, we failed our users?)
My point is, since the use of .resize_with is very rare, I'm not convinced adding further sugar for it is worth it.
I think the point is that there are several other _default() methods so it should be present for consistency and discoverability, even if it is rarely used.
I'm pretty sure i128 is very rarely used but it would be weird to omit it when u128 exists.
Reacted by Lokathor, Jaro Fietz, Nathan West, Clément Renault, Yari, Zakarum, Chris Bouchard and Kolsky
@zakarumych That's currently done for vec![0; N], so I suspect it could then be done for .resize(n, 0) without needing a separate method. (That said, it might be interesting to have a .resize_zeroed(n) or new_zeroed(n) or something once safe transmute can give a trait bound for which it's safe to initialize elements with zeros.)
@scottmcm In generic environment, with 'Vec<T>' where 'T: Default', you can use either '.resize_default()' or '.resize_with(Default::default())', the former can be specialized for integers and later not.
A-slice-vecArea: `[T]` and `Vec<T>`B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs-SmallLibs issues that are considered "small" or self-containedT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Activity
clarfonthey commentedon May 5, 2017
This would be an interesting addition for types which support
Defaultbut notClone. I'll try and make a PR for it.In the future, just FYI, feature requests should go in the RFCs repository.
joshlf commentedon May 5, 2017
Ah OK, noted. That's even for feature requests like this one that don't deserve an RFC?
clarfonthey commentedon May 15, 2017
Yep! The general rule is either PR here or RFC elsewhere.
Also, @joshlf would you mind renaming this issue to "Tracking issue for Vec::resize_default" ?
And @nikomatsakis would you mind tagging it?
[-]Add Vec::resize_default method[/-][+]Tracking issue for Vec::resize_default[/+]joshlf commentedon May 15, 2017
Gotcha; thanks!
Rollup merge of rust-lang#41771 - clarcharr:resize_default, r=nikomat…
Auto merge of #41771 - clarcharr:resize_default, r=nikomatsakis
Mark-Simulacrum commentedon Jan 17, 2018
@rust-lang/libs Nominating for stabilization. The only potential concern is that we can generalize this to take a closure (see below). I don't know that it'd be all that useful, though, so I'm inclined to say that we shouldn't do so, and
CloneandDefaultare the two "producing" traits in std, so standardizing around them seems logical.clarfonthey commentedon Jan 18, 2018
@Mark-Simulacrum I'd be willing to make a PR that adds
resize_withunder a separate feature flag. I think that all three are useful;resize_defaultwould simply be documented as equivalent toresize_with(Default::default)sfackler commentedon Jan 18, 2018
I might lean more towards just having
resize_with, for now at least. It's strictly more powerful and doesn't seem all that more verbose thanresize_default?73 remaining items
lambda-fairy commentedon Apr 15, 2020
Thanks for the info @xfix.
If the majority of calls can use
Default::defaultthen that would support having a specialized method for that case.Shnatsel commentedon Apr 15, 2020
For completeness I've scanned crates.io for uses of
.resize_with, exact command used wasrg --iglob '*.rs' -F '.resize_with'. Here are the results: https://pastebin.com/UgpzzQxTAt a glance the results seem to align with the ones posted above - roughly half of the invocations just use the default value. On the other hand, there are just 207 uses of
resize_withacross the entire ecosystem, so I'm not sure adding another method for a relatively niche use case is worth it.Shnatsel commentedon Apr 15, 2020
For reference,
unwrap_or_elseoccurs 24631 times andunwrap_or_default6286 times.SoniEx2 commentedon Apr 15, 2020
for reference, how many unwrap_or with the default value for the type (
0,"", etc)? how many unwrap_or_else with Default::default? is it possible that the problem is elsewhere? (bad docs, bad book, we failed our users?)Shnatsel commentedon Apr 15, 2020
Here are all occurrences of
.resize*and.unwrap_or*methods for your perusal:resize.gz unwrap_or.gz
My point is, since the use of
.resize_withis very rare, I'm not convinced adding further sugar for it is worth it.Timmmm commentedon Jul 27, 2020
I think the point is that there are several other
_default()methods so it should be present for consistency and discoverability, even if it is rarely used.I'm pretty sure
i128is very rarely used but it would be weird to omit it whenu128exists.zakarumych commentedon Nov 17, 2020
resize_defaultcan be specialized forVec<{integer}>usingAllocRef::grow_zeroedscottmcm commentedon Nov 19, 2020
@zakarumych That's currently done for
vec![0; N], so I suspect it could then be done for.resize(n, 0)without needing a separate method. (That said, it might be interesting to have a.resize_zeroed(n)ornew_zeroed(n)or something once safe transmute can give a trait bound for which it's safe to initialize elements with zeros.)zakarumych commentedon Nov 19, 2020
@scottmcm In generic environment, with 'Vec<T>' where 'T: Default', you can use either '.resize_default()' or '.resize_with(Default::default())', the former can be specialized for integers and later not.
joshtriplett commentedon Jul 16, 2022
resize_withhas been shipped in stable, andresize_defaultwas deprecated and removed in favor ofresize_with.