Skip to content

Commit

Permalink
Add runtime/compile time env. var. config/usage
Browse files Browse the repository at this point in the history
  • Loading branch information
simsekgokhan committed Apr 17, 2024
1 parent 2499538 commit 1827497
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ rustflags = ["-C", "link-arg=-fuse-ld=lld"] # sudo apt -y install lld

[target.'cfg(target_os = "macos")']
# For faster link times ($ brew install michaeleisel/zld/zld )
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld"]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld"]

# Run time env. vars. see main.rs for usage
[env]
FOO = "bar"
XX = "42"
PATH_TO_SOME_TOOL = { value = "bin/tool", relative = true }
USERNAME = { value = "test_user", force = true }
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml"
],
"rust-analyzer.showUnlinkedFileNotification": false
}
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ clap = "4.0.32"
[features]
foo = [] # feature has no explicit dependencies

# Todo: how to use them?
# Compile time env. vars
# To use at runtime, ss .cargo/config.toml
[env]
RUST_BACKTRACE="1"
AA=1
BB=true
FOO = "bar"
XX = 42
PATH_TO_SOME_TOOL = { value = "bin/tool", relative = true }
USERNAME = { value = "test_user", force = true }

# # All workspace members should inherit these keys for package declarations.
# [workspace.package]
# authors = ["Aptos Labs <[email protected]>"]
Expand Down
11 changes: 11 additions & 0 deletions src/env.vars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// Run time env. vars use
// .cargo/config.toml
// [env]
// ...
// see main.rs for usage

// Todo
// Compile time env. vars use
// Cargo.toml
// [env]
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ mod closure;

fn main() {

// 0) Use runtime env. vars. See [env] in Cargo.toml
let xx: i32 = env!("XX").parse().unwrap();
println!("xx: {:?}", xx); // 42


// 1) Minigrep ---------------------------------------------
// Usage:
// cargo r search-str file.txt
Expand Down
6 changes: 6 additions & 0 deletions src/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
// assert_eq!(32, 32);
// }


#[test]
fn ex1_ww() {
// println!("xx: {:?}", xx);
assert_eq!(42, 42);
}

0 comments on commit 1827497

Please sign in to comment.