You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A const can be thought of as a #define in C: it has metadata overhead but it has no runtime overhead. “Should I use a #define or a static in C,” is largely the same question as whether you should use a const or a static in Rust.
Unfortunately, this is rather misleading. An experienced C programmer will try and avoid #define as much as possible (they do have their use) because:
a #define is unscoped, and the C preprocessor is just a text preprocessor
a #define is untyped
You can check constants - "static const" vs "define" in C for more context and relevant advices; specifically Jonathan Leffer's answer (yep, just skip mine). Note: in C++, the answer is static const because C++ lifts a lot of restrictions for where one can use static const variables.
Since in Rust const is scoped and typed, it is really no equivalent to #define. It is, however, much closer to enum.
Maybe simply comparing to enum instead of #define would avoid confusing the reader?
The text was updated successfully, but these errors were encountered:
I think it would be fine to add some clarification here, but the emphasis is that the definition is inlined everywhere (like #define), not so much the odd semantics of #define
At the end of Rust Book chapter 5.27
const
andstatic
is the following extract:Unfortunately, this is rather misleading. An experienced C programmer will try and avoid
#define
as much as possible (they do have their use) because:#define
is unscoped, and the C preprocessor is just a text preprocessor#define
is untypedYou can check constants - "static const" vs "define" in C for more context and relevant advices; specifically Jonathan Leffer's answer (yep, just skip mine). Note: in C++, the answer is
static const
because C++ lifts a lot of restrictions for where one can usestatic const
variables.Since in Rust
const
is scoped and typed, it is really no equivalent to#define
. It is, however, much closer toenum
.Maybe simply comparing to
enum
instead of#define
would avoid confusing the reader?The text was updated successfully, but these errors were encountered: