# Constructing String(x::Vector{UInt8}) empties the vector?

**URL:** <https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816>\
**Category:** Internals & Design\
**Tags:** bug\
**Created:** [April 8, 2021, 10:03am UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816 "2021-04-08T10:03:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![green.nsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/green.nsk/32/24014_2.png) [@green.nsk](https://discourse.julialang.org/u/green.nsk)\
**Post date:** [April 8, 2021, 10:03am UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/1 "2021-04-08T10:03:47Z")

</div>

```julia
data = Vector{UInt8}("hello, world!")
@assert(!isempty(data))
String(data)
@assert(!isempty(data)) # boom!

```

is this expected?

I found this issue: [https://github.com/JuliaLang/julia/issues/24388](https://github.com/JuliaLang/julia/issues/24388), which eventually was resolved by copying data when converting string to a vector. Perhaps, when constructing a string from a vector, it should also copy?

---

<div class="post-metadata">

**Author:** ![green.nsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/green.nsk/32/24014_2.png) [@green.nsk](https://discourse.julialang.org/u/green.nsk)\
**Post date:** [April 8, 2021, 10:58am UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/2 "2021-04-08T10:58:10Z")

</div>

Actually, after reading that thread more thoroughly, Jeff Bezanson suggested (and implemented?) precisely this behaviour: [Conversion of String to Vector{UInt8} · Issue #24388 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/24388#issuecomment-341244445)

So I guess this is “by design” because Julia doesn’t have move().

---

<div class="post-metadata">

**Author:** ![green.nsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/green.nsk/32/24014_2.png) [@green.nsk](https://discourse.julialang.org/u/green.nsk)\
**Post date:** [April 8, 2021, 11:16am UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/3 "2021-04-08T11:16:10Z")

</div>

Still, it’s mind-blowing that String constructor is allowed to do that.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 8, 2021, 2:25pm UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/4 "2021-04-08T14:25:45Z")

</div>

I agree this is surprising, but it _is_ explicitly documented:

```julia
help?> String
search: String string StringIndexError Cstring Cwstring bitstring SubString include_string setrounding unsafe_string AbstractString escape_string unescape_string AbstractUnitRange SubstitutionString setprecision AbstractIrrational

  String(v::AbstractVector{UInt8})

  Create a new String object from a byte vector v containing UTF-8 encoded characters. If v is Vector{UInt8} it will be truncated to zero length and future modification of v cannot affect the contents of the resulting string. To avoid
  truncation of Vector{UInt8} data, use String(copy(v)); for other AbstractVector types, String(v) already makes a copy.

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [April 8, 2021, 2:31pm UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/5 "2021-04-08T14:31:01Z")

</div>

I guess it’s slightly out of convention. Proper name should be `String!`, then there would be no confusion.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 8, 2021, 4:44pm UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/6 "2021-04-08T16:44:57Z")

</div>

If you want a non-destructive string view of a byte array, another option is [https://github.com/JuliaStrings/StringViews.jl](https://github.com/JuliaStrings/StringViews.jl)

---

<div class="post-metadata">

**Author:** ![green.nsk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/green.nsk/32/24014_2.png) [@green.nsk](https://discourse.julialang.org/u/green.nsk)\
**Post date:** [April 8, 2021, 6:11pm UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/7 "2021-04-08T18:11:38Z")

</div>

The problem is people will read this documentation after their tests fail in a weird way, not before (if they have tests, that is).

---

<div class="post-metadata">

**Author:** ![dcasbol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dcasbol/32/18756_2.png) [@dcasbol](https://discourse.julialang.org/u/dcasbol)\
**Post date:** [May 10, 2023, 7:11am UTC](https://discourse.julialang.org/t/constructing-string-x-vector-uint8-empties-the-vector/58816/8 "2023-05-10T07:11:25Z")

</div>

Hi. I am one of those people =) I agree that it should follow the expected behaviour and, if otherwise, better be called `String!`. I don’t think it matters much that it is documented, since the level of perplexity of such behaviour is so large. Nobody would check the constructor of strings before anything failing.
