# One-element vector (w/o allocations)

**URL:** <https://discourse.julialang.org/t/one-element-vector-w-o-allocations/94922>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 20, 2023, 4:43pm UTC](https://discourse.julialang.org/t/one-element-vector-w-o-allocations/94922 "2023-02-20T16:43:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 20, 2023, 4:43pm UTC](https://discourse.julialang.org/t/one-element-vector-w-o-allocations/94922/1 "2023-02-20T16:43:22Z")

</div>

Is there a package for a non-allocating representation of a single-element vector? I am basically thinking of the equivalent of

```julia
struct OneElementVector{T} <: AbstractVector{T}
    x::T
end

Base.size(v::OneElementVector) = (1, )

Base.getindex(v::OneElementVector, i::Int) = i == 1 ? v.x : throw(BoundsError(v, i))

```

`Base.RefValue` is not the same because it requires no index,while `StaticArrays.SVector(x)` is overkill.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 20, 2023, 4:57pm UTC](https://discourse.julialang.org/t/one-element-vector-w-o-allocations/94922/2 "2023-02-20T16:57:06Z")

</div>

> **[GitHub - JuliaFolds/MicroCollections.jl: Immutable empty and singleton...](https://github.com/JuliaFolds/MicroCollections.jl)**
>
> Immutable empty and singleton collections. Contribute to JuliaFolds/MicroCollections.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [February 20, 2023, 6:36pm UTC](https://discourse.julialang.org/t/one-element-vector-w-o-allocations/94922/3 "2023-02-20T18:36:12Z")

</div>

`SVector{1,T}` from `StaticArrays.jl` would also work for this, but might be overkill for your needs.
