E3043

E3043 — A for loop over a reference to a collection, for x in &v. Rust hands out references to the items there, which no iterator here does yet: write v.iter() to read the items, or for x in v to consume the collection.

What the compiler reports

Error[E3043] at 21:13: a `for` loop cannot go over a reference to Vec<i64> yet

The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.

A program that triggers it

//
// Part of the Vx Project, under the Apache License v2.0 with LLVM Exceptions.
// See LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
// `for x in &v` is refused with a pointer to what to write instead. In Rust it hands out
// a reference to each item, which no iterator here does yet. It used to crash the compiler.
import core::iter;
import std::vec;

fn main() -> i32 {
  let mut v = Vec<i64>::new();
  v.push(40i64);
  v.push(2i64);
  let mut s = 0i64;
  for x in &v {
    s = s + x;
  }
  print(s);
  return 0;
}

From tests/frontend/fail/for_loop_over_a_reference_to_a_vec.vx, which asserts this diagnostic on every commit.

Related