leshow
leshow

Reputation: 1648

How do I change from as_slice() to [] syntax?

Recompiling some older code against the newer nightlies, I'm getting warnings on using the as_slice() syntax in favour of the var[] syntax.

However when I replace as_slice() with [] as shown in the RFC, I get a compiler error saying:

 expected `&str`,
    found `str`
(expected &-ptr,
    found str) [E0308]
src/main.rs:38         print_usage(program[], opts);

compared to my original

print_usage(program.as_slice(), opts);

Is the as_slice() syntax going away entirely, or is it just more idiomatic to write it as vec[]? What's the deal with the error I'm getting when I follow what the compiler is asking me to do?

Upvotes: 4

Views: 1795

Answers (1)

swizard
swizard

Reputation: 2701

You were near to success:

print_usage(&program[], opts);

So yes, now we should use square brackets syntax as &[start .. end] or &mut [start .. end] instead of as_slice/slice/slice_from/slice_to.

Upvotes: 5

Related Questions