Skip to content

env_or_default macro #48952

Closed as not planned
Closed as not planned
@vbrandl

Description

@vbrandl

I find my self doing something like this quite often:

const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");

fn main() {
    println!("{}", VERSION.unwrap_or("unknown version"));
}

I think it would be nice to have a env_or_default macro that defaults to a fallback string at compile time if the environment variable cannot be found. I'm thinking of something like

const VERSION: &str = env_or_default!("CARGO_PKG_VERSION", "unknown version");

I know the runtime cost isn't that high but if it can be done at compile time, it should be done.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions