Created
April 20, 2026 10:30
-
-
Save leobm/0eb886ba9c932ec2b5904e2218fd6ce0 to your computer and use it in GitHub Desktop.
cargo scripts examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S cargo +nightly -Zscript -q | |
| ---cargo | |
| [package] | |
| edition = "2024" | |
| [dependencies] | |
| rand = "0.9.2" | |
| clap = { version = "4", features = ["derive"] } | |
| --- | |
| use clap::Parser; | |
| use rand; | |
| //use std::env; | |
| #[derive(Parser)] | |
| struct Args { | |
| /// Name to greet | |
| #[arg(short, long, default_value = "Stranger")] | |
| name: String, | |
| /// Number of times to greet | |
| #[arg(short, long, default_value_t = 1)] | |
| count: u8, | |
| } | |
| fn upper_greet(name: &str) -> String { | |
| format!("Hello {}", name.to_uppercase()) | |
| } | |
| fn main() { | |
| //let args: Vec<String> = env::args().collect(); | |
| //println!("{:?}", args); // args[0] ist Programmname | |
| //let name = args.get(1).map_or("Stranger", String::as_str); | |
| let args = Args::parse(); | |
| let x: u8 = rand::random(); | |
| println!("Random number: {x}"); | |
| let my_greet = upper_greet(&args.name); | |
| println!("{}", my_greet) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S cargo +nightly -Zscript -q | |
| ---cargo | |
| [package] | |
| edition = "2024" | |
| [dependencies] | |
| reqwest = { version = "0.12", features = ["json"] } | |
| tokio = { version = "1", features = ["macros", "rt-multi-thread"] } | |
| anyhow = "1" | |
| --- | |
| use anyhow::{Context, Result, bail}; | |
| use std::collections::HashMap; | |
| #[tokio::main] | |
| async fn main() -> Result<()> { | |
| let resp = reqwest::get("https://httpbin.org/ip") | |
| .await | |
| .context("Failed to connect to httpbin.org")? | |
| .json::<HashMap<String, String>>() | |
| .await | |
| .context("Failed to parse JSON response")?; | |
| let origin = resp | |
| .get("origin") | |
| .context("Response missing 'origin' field")?; | |
| if origin.is_empty() { | |
| bail!("Origin IP is empty!"); | |
| } | |
| println!("Your IP: {}", origin); | |
| Ok(()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "rust-analyzer.cargo.buildScripts.enable": true, | |
| "rust-analyzer.procMacro.enable": true, | |
| "rust-analyzer.cargo.extraEnv": { | |
| "RUSTUP_TOOLCHAIN": "nightly" | |
| }, | |
| "rust-analyzer.server.extraEnv": { | |
| "RUSTUP_TOOLCHAIN": "nightly" | |
| }, | |
| "rust-analyzer.linkedProjects": [ | |
| "example_cargo_script.rs", | |
| "example_http_request.rs" | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment