translate this in a standalone rust code that i can run withotu setting up a project…
- User
translate this in a standalone rust code that i can run withotu setting up a project
filename: concurrent_commune_lookup.py
import asyncio import aiohttp import time
class CommuneLookup: def init(self, base_url: str): self.base_url = base_url
async def fetch(self, session: aiohttp.ClientSession, code: int): url = f"{self.base_url}?code_commune={code}" start = time.perf_counter() print(f"Lookup for code_commune={code}") async with session.get(url) as response: pass elapsed = time.perf_counter() - start print(f"Lookup start for code_commune={code}: {elapsed:.3f}s") async def lookup_all(self, start: int = 0, end: int = 90): async with aiohttp.ClientSession() as session: tasks = [ self.fetch(session, code) for code in range(start, end + 1) ] await asyncio.gather(*tasks)if name == "main": lookup = CommuneLookup("http://localhost:8080/records") asyncio.run(lookup.lookup_all(0, 1000))
Assistant
Here's the standalone Rust version of your Python script. This Rust code performs the same concurrent HTTP GET requests to a given base URL, measuring how long each request takes.
You can save this as a
.rsfile and run it directly usingrustc(Rust compiler) without needing to set up aCargo.tomlproject, as long as you have Rust installed (rustcandcargoin your PATH).
✅ Standalone Rust Code (no Cargo project needed)
use std::time::{Instant}; use reqwest::Client; use tokio; #[tokio::main] async fn main() { let base_url = "http://localhost:8080/records"; let start = 0; let end = 1000; let client = Client::new(); let mut tasks = Vec::new(); for code in start..=end { let client = client.clone(); let url = format!("{}?code_commune={}", base_url, code); tasks.push(tokio::spawn(async move { println!("Lookup for code_commune={}", code); let start_time = Instant::now(); let resp = client.get(&url).send().await; let elapsed = start_time.elapsed().as_secs_f64(); match resp { Ok(_) => println!("Lookup completed for code_commune={}: {:.3}s", code, elapsed), Err(e) => println!("Lookup failed for code_commune={}: {} (took {:.3}s)", code, e, elapsed), } })); } for task in tasks { task.await.unwrap(); } }
📦 How to Run
- Install Rust (if you haven't already):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh-
Save the code above into a file named, for example:
commune_lookup.rs -
Compile and run it:
rustc -O --crate-type=bin --extern reqwest --extern tokio commune_lookup.rs ./commune_lookup⚠️ Note: The above assumes you have
reqwestandtokioalready compiled in your~/.cargo/registrycache. If not, you'll need to create a Cargo project to pre-download the dependencies.
💡 If You Want to Avoid Cargo Entirely
Rust is not designed for fully self-contained scripts like Python’s
importmodel. To truly run this without Cargo, you'd need to:- Compile
reqwestandtokiomanually from source. - Use a tool like
cargo-expandorcargo-srcto inline dependencies.
But if you're okay with having
cargoinstalled (which is the standard Rust toolchain), you can just create a minimal project:
🧱 Optional: Minimal Cargo Project (Recommended)
- Create a new project:
cargo new commune_lookup cd commune_lookupCargo.toml:
[package] name = "commune_lookup" version = "0.1.0" edition = "2021" [dependencies] reqwest = { version = "0.11", features = ["json"] } tokio = { version = "1", features = ["full"] }-
Replace
src/main.rswith the Rust code above. -
Run:
cargo runLet me know if you want a version that works without any external crates (
reqwest,tokio) or even without internet access!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.