为什么 if then return break end 在 for 循环内的 Lua 中不起作用

Why does if then return break end not work in Lua inside for loop

我有以下函数检查给定参数是否作为键值 table 中的键找到。如果是这种情况,它应该 return true 并跳出循环。如果什么都没找到,则什么都不做。

function checkId(id)
  for k,v in pairs(info) do
    if id == tostring(k) then
      return true
      break -- break out of loop. mission accomplished.
    end
  end
end

我得到一个

'end' expected (to close 'do' at line 192) near 'break'

当我尝试 运行 这个脚本时。我错过了什么?

逻辑上你不能return break那样。

return 立即退出该函数(因此您不需要 break)。

该特定错误是因为 lua return 必须是块中的最后一条语句。