11

I am writing a test to see the time difference on different buffer sizes when reading from a stream. Instead of changing the buffer size everywhere in the code, it would be nice to have some preprocessor doing it for me so that I will only need to change the value in one place.

An example of what I'm thinking of is writing a C macro define BUFFER 1024, and when creating a array using it to define the size.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Olof
  • 776
  • 2
  • 14
  • 33
  • 1
    What you really want is a variable that can be used in a constant expression. A language should provide mechanisms for achieving that which are actually native and typesafe, rather than relying upon a preprocessor to naively substitute text everywhere. And as far as I can tell, Rust does. – underscore_d Jul 18 '17 at 09:47
  • 1
    @underscore_d The possible duplicate uses an outdated syntax. – Boiethios Jul 18 '17 at 09:50
  • 3
    @Olof: For future reference, this is a case of [the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378): you should ask about what you're doing (abstract out buffer size), not *how* you're attempting to do it (C preprocessor macros in Rust). – DK. Jul 18 '17 at 09:53
  • @Boiethios: Then it's time to update it! – Matthieu M. Jul 18 '17 at 10:07
  • @MatthieuM. I think it is not really a duplicate: the author of the other question seems to ask for a `sizeof` at compile-time. – Boiethios Jul 18 '17 at 10:15
  • 2
    @Boiethios: So... duplicates can be a bit funny that way. What matters is not so much what the question was about, but what the answers teach. That is, the guideline is to close A as duplicate of B whenever A is answered by the answers of B. *If you think this is not the case, please feel free to come to the chatroom and grab one of the other gold-badge users of Rust, so they can reopen the question.* – Matthieu M. Jul 18 '17 at 10:39
  • @Boiethios A lot of the problem is when people ask one question in the title and then another in the body. Which were they really asking? Which are other people going to be searching for? And what does the final answer end up being? I think in this case, it was basically the same in the end, albeit outdated as you mentioned. Which makes an update over there a nice idea! – underscore_d Jul 18 '17 at 10:41
  • @MatthieuM. Thanks for the explanation. I think that the sentence *This question was marked as an exact duplicate of an existing question.* is confusing. – Boiethios Jul 18 '17 at 12:33
  • @Boiethios: It is :( It's been brought up on multiple occasions on meta :( – Matthieu M. Jul 18 '17 at 13:20
  • There used to be more variations on the duplicate message, right? Like you could still flag it, but IIRC that would not always close the question and might instead just highlight it as "Possible duplicate". And those old reasons remain on old questions even if they are no longer selectable. – underscore_d Jul 18 '17 at 15:43

1 Answers1

13

Use const to fulfill your need:

const BUFFER: usize = 512;

However, this is not preprocessor: as underscore_d's comment says, the usage of preprocessor is a pretty archaic mechanism. It has been replaced in Rust with:

  • const in case of literal value;
  • macros, to generate code.

You can understand the Rust's const keyword as "evaluated at compile-time". The (maybe) incoming functions evaluated at compile-time will be marked as const also.

Furthermore, even in C, using the preprocessor to create a compile-time constant is not the best practice.

Boiethios
  • 38,438
  • 19
  • 134
  • 183
  • It works, took me some time to understand. The compiler needs to know the size of an array during compiling but since a constant never changes the compiler knows the size to use. – Olof Jul 18 '17 at 10:23
  • 2
    @Olof Exactly. `const` in Rust means "known at compile-time". I edited my answer to add that. – Boiethios Jul 18 '17 at 10:26