#Question about iterating a list of strings

13 messages · Page 1 of 1 (latest)

pastel pasture
#

Hi,
I've got a background in Rust (10 years) and in Python (1 year) and I try to learn Mojo.
I've try this :

fn main():
  try:
    var stuff = String("dddd\nffff\njjjj\nkkkk").split("\n")
    for x in stuff:
      print(x)
  except:
    print("failed")

and got this obscure error message :

/source/prog.mojo:7:12: error: no matching function in call to 'print'
      print(x)
      ~~~~~^~~
/source/prog.mojo:1:1: note: candidate not viable: expected at most 0 positional arguments, got 1
fn main():
^
/source/prog.mojo:1:1: note: candidate not viable: callee expects 2 parameters, but 0 were specified
fn main():
^
/source/prog.mojo:7:13: note: failed to infer parameter 'T', argument type 'Reference[String, 1, stuff, 0]' does not conform to trait 'Stringable'
      print(x)
            ^
mojo: error: failed to parse the provided Mojo source module

but if I use indexes, it works :

for x in range(0, len(stuff)):
      print(stuff[x])

I seem to have a hard time with the most basic things 🙂

What have I misunderstood ?

Thanks

somber forgeBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

plucky wharf
#

You need to use print(x[])

#

for 'dereference' or something like that. I've seen it in the other code, but still don't understand this model yet by myself)

pastel pasture
#

thanks !

somber forgeBOT
foggy walrus
# pastel pasture thanks !

Auto-deref has been implemented and will slowly get added to the stdlib so you wouldn't need to deference pointers yourself in the future

pastel pasture
spark sky
native hamlet
foggy walrus
#

Also, the square bracket acts as a getter.
I believe ref[] is the same as ref[0] or something like that