E0017

E0017#

The usage of identifier is ambiguous. If it refers to the loop variable, please use as <id> to bind it in the pattern. If it refers to the original value of the variable before entering the loop, please bind it to a new binder outside the loop.

错误示例#

fn main {
  let a : @string.View = "asdf"
  loop a {
    [_, .. d] => {
      println(a)
      //      ^
      // Warning: The usage of 'a' is ambiguous. If it refers to the loop
      // variable, please use `as a` to bind it in the pattern. If it refers to
      // the original value of 'a' before entering the loop, please bind it to a
      // new binder outside the loop.
      continue d
    }
    [] => ()
  }
}

The output is:

asdf
asdf
asdf
asdf

Because a refers to the value of a before entering the loop, therefore the value is always the same.

建议#

It is often the case that you may want to refer to the loop variable that changes with loop iterations. If so, use as <id> to bind it in the pattern.

fn main {
  let a : @string.View = "asdf"
  loop a {
    [_, .. d] as a => {
      println(a)
      continue d
    }
    [] => ()
  }
}

Output:

asdf
sdf
df
f

Or, if you want to refer to the original value of the variable before entering the loop, explicitly bind it to another name outside the loop.

fn main {
  let a : @string.View = "asdf"
  let b = a
  loop a {
    [_, .. d] => {
      println(b)
      continue d
    }
    [] => ()
  }
}

Output:

asdf
asdf
asdf
asdf