Skip to content

Mojo Error: value of type 'X' cannot be implicitly copied

error: value of type 'List[Int]' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable' means your code would make a copy of a value that Mojo only copies when you ask. It shows up with List, Dict, String lists, your own structs and generic types T. You fix it by choosing what you really want: move the value with ^, copy it with .copy(), or just look at it through a reference. For the other errors in this series, see Mojo Errors Explained.

def main():
var a: List[Int] = [1, 2, 3]
var b = a
print(b)
copy.mojo:3:13: error: value of type 'List[Int]' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'
var b = a
^
copy.mojo:3:13: note: consider transferring the value with '^'
var b = a
^
^
copy.mojo:3:13: note: you can copy it explicitly with '.copy()'
var b = a
^
.copy()
mojo: error: failed to parse the provided Mojo source module

The two notes are the two most common fixes. The first shows ^ added after a; the second shows .copy() added after a.

In Python, b = a doesn’t copy a list. Both names point to the same list, and changing one changes the other. Mojo works differently: every variable owns its own value. So var b = a has to either make a full copy of the list, or move the list from a to b.

Copying a list can be slow when it’s big, so Mojo doesn’t do it silently. Only small, cheap types like Int, Float64, Bool and String are implicitly copyable (they conform to the ImplicitlyCopyable trait). For everything else, you say what you mean. The value semantics page of the Mojo manual explains this model, and our Mojo Value Semantics lesson walks through it with examples.

Fix 1: Move it with ^ when you’re done with the original

Section titled “Fix 1: Move it with ^ when you’re done with the original”

If you don’t need the old variable any more, transfer the value. a^ hands the list over to b, with no copy at all:

def main():
var a: List[Int] = [1, 2, 3]
var b = a^
print(b)

Output:

[1, 2, 3]

After the move, a is empty. Reading it again is a different error, use of uninitialized value ‘a’.

This is the right fix when you return a list you built in a function, or add a list to another collection:

def evens(n: Int) -> List[Int]:
var xs: List[Int] = []
for i in range(n):
if i % 2 == 0:
xs.append(i)
return xs^
def main():
var grid: List[List[Int]] = []
var row = evens(7)
grid.append(row^)
print(grid[0])

Output:

[0, 2, 4, 6]

Without the ^, both return xs and grid.append(row) give this error. See Mojo Transfer for more on ^.

Fix 2: Copy it with .copy() when you need both

Section titled “Fix 2: Copy it with .copy() when you need both”

If you really want two independent lists, ask for the copy:

def main():
var a: List[Int] = [1, 2, 3]
var b = a.copy()
b.append(4)
print(a)
print(b)

Output:

[1, 2, 3]
[1, 2, 3, 4]

b is a separate list, so appending to it doesn’t change a. That’s different from Python, where b = a would have changed both.

Fix 3: Read through a reference instead of copying

Section titled “Fix 3: Read through a reference instead of copying”

Often you didn’t want a copy at all; you only wanted to look at something. Taking an item out of a list of lists with var first = lists[0] copies it, which gives this error. Use ref to get a reference to the item instead:

def main():
var lists: List[List[Int]] = [[1, 2], [3]]
ref first = lists[0]
print(first)
print(len(lists[1]))

Output:

[1, 2]
1

ref first is another name for the item inside lists, so nothing is copied. Functions work the same way by default: an argument like nums: List[Int] is a read-only reference, so total(my_list) never copies. See Mojo Argument Conventions.

This one catches nearly everyone writing a struct that holds a list:

struct Playlist:
var songs: List[String]
def __init__(out self, songs: List[String]):
self.songs = songs
def main():
var mine: List[String] = ["Intro", "Outro"]
var p = Playlist(mine)
print(len(p.songs))
playlist.mojo:5:22: error: value of type 'List[String]' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'
self.songs = songs
^~~~~
playlist.mojo:5:22: note: consider transferring the value with '^'
self.songs = songs
^
^
playlist.mojo:5:22: note: you can copy it explicitly with '.copy()'
self.songs = songs
^
.copy()
mojo: error: failed to parse the provided Mojo source module

songs is only a read-only reference here, so the struct can’t keep it without a copy. Mark the argument var, so the struct gets its own value, and move it into the field. The caller then moves its list in with ^ (or passes mine.copy() to keep its own):

struct Playlist:
var songs: List[String]
def __init__(out self, var songs: List[String]):
self.songs = songs^
def main():
var mine: List[String] = ["Intro", "Outro"]
var p = Playlist(mine^)
print(len(p.songs))

Output:

2

@fieldwise_init writes this kind of initializer for you (with var arguments). The caller still has to choose: Playlist(mine) with a list variable gives the same error at the call, so write Playlist(mine^) or Playlist(mine.copy()). See Mojo Structs.

  • var b = a with a List, Dict or Set.
  • Returning a local list from a function: return xs^.
  • Putting a list inside another collection: grid.append(row^), or row.copy() if you still need row.
  • Your own structs. var b = a with a struct that holds a List says value of type 'Team' cannot be implicitly copied. Give the struct the Copyable trait and call .copy(), or move it. See Mojo Common Traits.
  • Generic code. value of type 'T' cannot be implicitly copied in a generic function means you need .copy() (with T: Copyable) or a move. See Mojo Generics.

Keep an unsorted backup of the list, sort the original, and print both. Fix the error without moving scores away.

def main():
var scores: List[Int] = [30, 10, 20]
var backup = scores
sort(scores)
print(backup)
print(scores)

Hint: you need two independent lists here, so moving with ^ won’t do.

Show solution
def main():
var scores: List[Int] = [30, 10, 20]
var backup = scores.copy()
sort(scores)
print(backup)
print(scores)

Output:

[30, 10, 20]
[10, 20, 30]

.copy() makes a separate list, so sorting scores leaves backup as it was. With scores^, scores would be empty and the sort line would fail.

  • Mojo only copies cheap types like Int and String silently; List, Dict and most structs need you to choose.
  • ^ moves the value: no copy, and the old variable is empty afterwards.
  • .copy() makes an independent copy when you need both.
  • ref and normal function arguments let you look without copying.
  • In an initializer, take the argument as var and store it with ^.

Next up: use of uninitialized value ‘x’, the error you get after a move, or before a value is set.

Quick check

1. You build a List in a function and want to return it. What do you write?
2. After var b = a.copy() and b.append(4), what is in a (which was [1, 2])?
3. Which type can you copy with var b = a without any error?