# Global mutable slice?

**URL:** <https://ziggit.dev/t/global-mutable-slice/5983>\
**Category:** Help\
**Tags:** memory-management\
**Created:** [September 14, 2024, 1:02am UTC](https://ziggit.dev/t/global-mutable-slice/5983 "2024-09-14T01:02:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![david\_vanderson](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@david\_vanderson](https://ziggit.dev/u/david_vanderson)\
**Post date:** [September 14, 2024, 1:02am UTC](https://ziggit.dev/t/global-mutable-slice/5983/1 "2024-09-14T01:02:33Z")

</div>

In the examples file for my gui library, I want to declare a global mutable slice (`[]u8`) to use in a text entry field that starts filled with some example text. Here’s the best I’ve come up with:

```zig
var mutable = "abc".*;
var mutable_slice = &mutable; // mutable_slice is []u8

```

Is there any way to make this a single line?

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 14, 2024, 1:29am UTC](https://ziggit.dev/t/global-mutable-slice/5983/2 "2024-09-14T01:29:40Z")

</div>

Hey @david_vanderson, check out this thread here for some related information: [How to anonymously initialize a slice?](https://ziggit.dev/t/how-to-anonymously-initialize-a-slice/5687)

In short, you cannot make a literal mutable. The thing that assigns from a literal (the copy) can be mutable but the literal itself is not. Because of that, you’re basically going to initialize some memory (like you do on the first line) and then grab a slice to that memory (second line).

If you point directly at the literal, you need to have a constant pointer (non-mutable).

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [September 14, 2024, 1:34am UTC](https://ziggit.dev/t/global-mutable-slice/5983/3 "2024-09-14T01:34:01Z")

</div>

> [@david\_vanderson](#):
>
> `var mutable_slice = &mutable;`

Also, just to point out… the `var` here means that the slice itself is mutable (you can redirect it at different memory), but you can do this:

```zig
const slice: []u8 = buffer[0..]; 

```

And you can still modiffy the underlying buffer. See this for more info: [Mutable and Constant Pointer Semantics](https://ziggit.dev/t/mutable-and-constant-pointer-semantics/3172)

---

<div class="post-metadata">

**Author:** ![david\_vanderson](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@david\_vanderson](https://ziggit.dev/u/david_vanderson)\
**Post date:** [September 14, 2024, 2:15am UTC](https://ziggit.dev/t/global-mutable-slice/5983/4 "2024-09-14T02:15:32Z")

</div>

I thought maybe there was some clever way to use a comptime block that I hadn’t been able to figure out, but sounds like no. Your explanation is very good, and I had not considered making the mutable slice const. Thank you very much!

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [September 14, 2024, 2:18am UTC](https://ziggit.dev/t/global-mutable-slice/5983/5 "2024-09-14T02:18:44Z")

</div>

EDIT: This code doesn’t work, see below.

> [@david\_vanderson](#):
>
> a comptime block

```zig
    const mut_slice = init: {
        var hidden_array = [_]u8{ 'a', 'b', 'c' };
        break :init hidden_array[0..];
    };
    std.debug.print("{s}\n", .{mut_slice});
    mut_slice[1] = 'z';
    std.debug.print("{s}\n", .{mut_slice});

```

---

<div class="post-metadata">

**Author:** ![david\_vanderson](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@david\_vanderson](https://ziggit.dev/u/david_vanderson)\
**Post date:** [September 14, 2024, 2:32am UTC](https://ziggit.dev/t/global-mutable-slice/5983/6 "2024-09-14T02:32:49Z")

</div>

I get this error (same error for me on 0.13):  
`% ../../apps/zig-macos-aarch64-0.14.0-dev.1550+4fba7336a/zig run test.zig test.zig:3:25: error: global variable contains reference to comptime var const mut_slice = init: {`

Here’s the full test.zig file:

```zig
const std = @import("std");

const mut_slice = init: {
        var hidden_array = [_]u8{ 'a', 'b', 'c' };
        break :init hidden_array[0..];
    };

pub fn main() !void {
    std.debug.print("mut: {s}\n", .{mut_slice});
}

```

---

<div class="post-metadata">

**Author:** ![dude\_the\_builder](https://ziggit.dev/user_avatar/ziggit.dev/dude_the_builder/32/557_2.png) [@dude\_the\_builder](https://ziggit.dev/u/dude_the_builder)\
**Post date:** [September 14, 2024, 2:57am UTC](https://ziggit.dev/t/global-mutable-slice/5983/7 "2024-09-14T02:57:07Z")

</div>

My bad, that was previously allowed but not any more. I guess you would have to at least have the two lines:

```zig
var array = [_]u8{ 'a', 'b', 'c' };
const mut_slice = array[0..];

```

---

<div class="post-metadata">

**Author:** ![david\_vanderson](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@david\_vanderson](https://ziggit.dev/u/david_vanderson)\
**Post date:** [September 14, 2024, 11:48am UTC](https://ziggit.dev/t/global-mutable-slice/5983/8 "2024-09-14T11:48:49Z")

</div>

Phew - I was worried I was missing something obvious. Thanks!
