Hard coded values

Does hardcoded value means that the value itself inside the binary instructions/binary code and not in memory location ?

Yeah, that's usually what it means. But it's not a clearly defined term, so you may have to read the context a bit to know what it actually is intending to imply.

1 Like

"Hard-coded" is a fairly vague term without any strong connection to runtime behavior guarantees like what you're describing. Usually people say a value is hard-coded if it appears in source code as a literal of some kind, as opposed to using code to compute it or fetch it from a config file or database. But that could easily include things like let x = 42; which may or may not have a memory address depending on all sorts of optimization settings.

I believe the precise guarantee you're asking about is the distinction between a Rust static and a Rust const, since statics are guaranteed to have a single address and consts are not. To quote the Reference:


Constants are essentially inlined wherever they are used, meaning that they are copied directly into the relevant context when used. References to the same constant are not necessarily guaranteed to refer to the same memory address.


A static item is similar to a constant, except that it represents a precise memory location in the program. All references to the static refer to the same memory location


thank you skysch

thx lxrec i think i get it

A hardcoded value is like this:
let variable = 5.
The code compiles and there is no way for you to change the value of variable.

A softcoded value is a value which you - often - can change after compilation time.
E.g the following pseudo code:

let variable = read_value_from_file("file_with_value.txt");

If I compile this code, the value of variable is whatever I put in the file file_with_value.txt. It's not constant.

so what about this

let x = 5 ;
let z = x ; // x is not a hard coded value  right  ?
// because we x need to be exist first at runtime

Well, x is a variable, not a value, so the question is already a category error.

And "hard-coded value" is still not a precise term with any objectively correct definition in Rust or any other language, but I would guess most people would agree that "the value stored in x is a hard-coded value", although it's a really weird thing to say since the "hard-coded-ness" of a value is kind of irrelevant at this level.

1 Like

no sorry i did a mistake , yes the value stored in x is hard coded but not the value stored in z , because the value stored in z depends on variable x and variable x doesnt exist at compile-time only exist after runtime right ? so the value of the variable z is not hard coded..

Well, by the time we're talking about the value of z, the whole concept of something being "hard-coded" has basically lost all meaning.

Hardcoded-ness is not a runtime property of a value like "even-ness" or "initialized-ness" that you can meaningfully propagate to other variables. It's a property of how the source code of the program was written (not even the compiled program, just the source code).


There are values that get computed as a program runs. Typical variables stored in RAM.

There are values that have to exist before your program can even start. For example:

let x = 5;

That "5" is pretty hard. It actually has to be part of the program binary somehow.

But typically now a days "hard coded" can have a different meaning. For example I might have some kind of network connection to make like so:

let con = some_protocol::connect("example.com:3455");

Well, just there I have "hard coded" the domain name and port into my code as a literal string. My program cannot be used to connect to any other server.

The alternative to "hard coding" would be to get that URL from a command line parameter or whatever user input. Thus making my code useful with many servers.


so hard coded value is not Not related to the evaluted of the value at runtime or compile time but it is related to the source code itself , we call the value hard coded if the value itself has been coded in the source code and doesnt need any external source , and we dont call the value hard coded if the value depends on any External source like user input or any other External files

Here's an example where the meaning gets fuzzy:

static IMG: &[u8] = include_bytes!("../path/to/file.png");

The image is part of the binary, but it's not part of the source code. This means that it can be easily changed without modifying the code, but then the program must be recompiled.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.