Ben Chuanlong Du's Blog

It is never too late to learn.

Rust Error "error: expected item, found 'let'"

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

The error message "error: expected item, found 'let'" is commonly encountered in Rust when you mistakenly place a let statement in an invalid location. A let statement can only be used within a function or a code block inside a function. It cannot be used placed directly within the body of a module, struct, trait or enum. Specially, you might mistakenly use let to define variables directly within a module where you really meant to define a static or const variable. This issue become more tricky if you use macros such as include! to literally include code from another file.

Comments