# Any (not empty) method for Vec

**URL:** <https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498>\
**Category:** Uncategorized\
**Created:** [March 8, 2023, 6:04pm UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498 "2023-03-08T18:04:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![madmaxio](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/madmaxio/32/10646_2.png) [@madmaxio](https://internals.rust-lang.org/u/madmaxio)\
**Post date:** [March 8, 2023, 6:04pm UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/1 "2023-03-08T18:04:44Z")

</div>

I saw there was big thread already about not\_empty method for Vec, but I personally think not having this method is a downside - user have to perform two operations instead of one, when writing code with !is\_empty(). In, for example C#, there is Any() method and it is very convenient. Not sure how Any() fits from english language semantics perspective, but at least google tells me it is possible to use it here.

---

<div class="post-metadata">

**Author:** ![elidupree](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/elidupree/32/4304_2.png) [@elidupree](https://internals.rust-lang.org/u/elidupree)\
**Post date:** [March 8, 2023, 6:11pm UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/2 "2023-03-08T18:11:13Z")

</div>

Previous discussion: [The is\_not\_empty() method as more clearly alternative for !is\_empty()](https://internals.rust-lang.org/t/the-is-not-empty-method-as-more-clearly-alternative-for-is-empty/10612)

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [March 8, 2023, 8:21pm UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/3 "2023-03-08T20:21:29Z")

</div>

I was going to say that `!vec.is_empty()` doesn't perform two operations because `is_empty` should be `#[inline]` but, [it isn't](https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#2072), and that's odd because `len` is `#[inline]`

---

<div class="post-metadata">

**Author:** ![fren\_gor](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fren_gor/32/9566_2.png) [@fren\_gor](https://internals.rust-lang.org/u/fren_gor)\
**Post date:** [March 8, 2023, 8:27pm UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/4 "2023-03-08T20:27:10Z")

</div>

Shouldn't the compiler still be able to inline it, since it is declared inside a generic impl?

---

<div class="post-metadata">

**Author:** ![quaternic](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quaternic/32/10440_2.png) [@quaternic](https://internals.rust-lang.org/u/quaternic)\
**Post date:** [March 8, 2023, 9:22pm UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/5 "2023-03-08T21:22:39Z")

</div>

> [@madmaxio](#):
>
> In, for example C#, there is Any() method and it is very convenient. Not sure how Any() fits from english language semantics perspective, but at least google tells me it is possible to use it here.

Wrt. naming, [Iterator::any](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.any) already exists, evaluating a predicate for each element until a true is found or the iterator is exhausted. It looks like [Any](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any) in C# is used the same way, but has an overload to be called without a predicate with the meaning that any element merely existing is sufficient.

With that, one way to write `!v.is_empty()` in Rust is `v.iter().any(|_| true)`.

> [@madmaxio](#):
>
> I saw there was big thread already about not\_empty method for Vec, but I personally think not having this method is a downside - user have to perform two operations instead of one, when writing code with !is\_empty().

Do you have any concrete examples where you would be asking if a Vec is empty or not? I'm sure sometimes that's exactly what you need because of some contextual meaning, but if you're doing that before possibly extracting an element or similar, code that instead branches on the success of such an operation may be an alternative. Something like these two:

```rust
// increment the last element, if any
if let [.., x] = &mut v[..] {
    *x += 1;
}

// pop and process elements until empty
while let Some(node) = v.pop() {
   v.extend(expand(node))
}

```

---

<div class="post-metadata">

**Author:** ![madmaxio](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/madmaxio/32/10646_2.png) [@madmaxio](https://internals.rust-lang.org/u/madmaxio)\
**Post date:** [March 9, 2023, 6:40am UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/6 "2023-03-09T06:40:17Z")

</div>

I was talking about user doing two operations with his hands when writing code (I mean i THOUGHT compiler will optimize it anyway), so thank you for information - if it doesn't.

---

<div class="post-metadata">

**Author:** ![madmaxio](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/madmaxio/32/10646_2.png) [@madmaxio](https://internals.rust-lang.org/u/madmaxio)\
**Post date:** [March 9, 2023, 6:43am UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/7 "2023-03-09T06:43:21Z")

</div>

> [@quaternic](#):
>
> Do you have any concrete examples where you would be asking if a Vec is empty or not? I'm sure sometimes that's exactly what you need because of some contextual meaning, but if you're doing that before possibly extracting an element or similar, code that instead branches on the success of such an operation may be an alternative. Something like these two

Well, for me there are pretty commons situations to do something when Vec is not empty - for example existing checks for records in a database or anywhere else, when you need just the fact it DOES exist, and not care about WHAT it is exactly inside.

---

<div class="post-metadata">

**Author:** ![madmaxio](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/madmaxio/32/10646_2.png) [@madmaxio](https://internals.rust-lang.org/u/madmaxio)\
**Post date:** [March 9, 2023, 6:47am UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/8 "2023-03-09T06:47:10Z")

</div>

> [@quaternic](#):
>
> Wrt. naming, [Iterator::any](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.any) already exists, evaluating a predicate for each element until a true is found or the iterator is exhausted. It looks like [Any](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.any) in C# is used the same way, but has an overload to be called without a predicate with the meaning that any element merely existing is sufficient

It is probably extension method there. I personally think "Any" sounds a bit strange for this in C#, but it is super convenient in this situation.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [June 7, 2023, 6:47am UTC](https://internals.rust-lang.org/t/any-not-empty-method-for-vec/18498/9 "2023-06-07T06:47:18Z")

</div>

Closed on June 7, 2023, 6:47am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
