I don't think there's an exact match for this concept in Ruby. Or, more accurately, we don't have one which uses const
expressions.
In rust, const
means fully compile time evaluated, and we don't yet have support for doing complex things at compile time. In the future, something like format!()
could become usable here. But until then, the computation needs to be performed at runtime.
This isn't necessarily a bad thing, though - the variables in ruby would have been evaluated at something approximating runtime anyways, and you can still get pretty good performance, as well as treating things as constants.
To that end, I suggest you use lazy_static
with format!()
. It'll be a bit more boilerplate than pure const
variables, but it allows for initializing each static on first use, and the usage looks exactly like using a regular static
or const
variable. Note that it isn't a const
- usages won't be replaced directly with the value. But I assume that in this case, that's fine.
This would look something like:
use lazy_static::lazy_static;
static PORT: &str = "3000";
lazy_static! {
static ref URL: String = format!("http://localhost:{}", PORT);
static ref HTTPS_URL: String = format!("https://localhost:{}", PORT);
}
I've included two variables using PORT
to highlight lazy_static
's nature of allowing multiple definitions in the same macro invocation. You could also include PORT
in the lazy_static!
block if it makes things clearer:
use lazy_static::lazy_static;
lazy_static! {
static ref PORT: &'static str = "3000";
static ref URL: String = format!("http://localhost:{}", *PORT);
static ref HTTPS_URL: String = format!("https://localhost:{}", *PORT);
}
There's a slight annoyance in that you need to explicitly dereference these statics with *
if you want to use them in a generic context, but I'm not sure there's a better way to do this. The *
is explicitly saying "I don't want the lazy_static initializing wrapper, I want its contents", and can be elided if its type is obvious. (we just need it here because format!()
accepts any type of thing to print).
If you want slightly clearer, but also potentially much more verbose code, maybe once_cell
with a url()
function providing access? once_cell
implements most of the functionality of lazy_static
, but without the macro magic. In some situations it can be clearer, but in others it isn't.