expression
File uploads: server-assigned storage keys
Separate the original display filename from the internal storage key. This pure model uses a trusted allocated integer ID, not user input, to derive a name. Real systems need unique allocation and collision-safe creation, access controls and storage outside publicly served directories. In-memory standalone Cargo example. Expected stdout (not executed): display: holiday.png storage: upload-00000042.bin Common mistake: Concatenating the client filename into the server storage path.
Formal Syntax & Structure
rust syntaxrust
format!("upload-{id:08}.bin")Working Example
Example Snippetrust
use axum::{Router, body::{Body, to_bytes}, http::{Request, StatusCode}};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { fn key(id: u64) -> String { format!("upload-{id:08}.bin") } let original_name = "holiday.png"; let storage_key = key(42); println!("display: {original_name}"); println!("storage: {storage_key}");}