Redact Secrets From Log Lines
Fold str::replace over known secrets so log lines mask tokens before printing, then assert the masked output contains none of them.
Code
fn redact(secret: &str, text: &str) -> String {
if secret.is_empty() {
return text.to_string();
}
text.replace(secret, "***")
}
fn main() {
let secrets = ["sk-live-9f2e1c", "user=42"];
let log_line = "auth ok token=sk-live-9f2e1c user=42 ip=10.0.0.7";
println!("raw : {log_line}");
// log lines never leak the secrets after masking
let mut safe = log_line.to_string();
for secret in secrets {
safe = redact(secret, &safe);
}
println!("safe: {safe}");
println!("clean: {}", secrets.iter().all(|s| !safe.contains(s)));
}How the snippet works
Read it this way: everything runs through the functions redact and main, doing all the work.
Iteration happens inside for secret in secrets — everything outside it runs once.
What to watch for
- It carries its own context — no imports beyond the runtime’s own surface.
Using it
Signature: fn redact(secret: &str, text: &str) -> String
The run prints labeled output — raw :, safe:, clean: — when the file runs.
Launch the file; main takes over and steps through redact.
Key statements
fn redact(secret: &str, text: &str) -> String {
if secret.is_empty() {
return text.to_string();
}
text.replace(secret, "***")
}
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.CSP Header BuilderBuild a Content-Security-Policy header from per-directive sources, skipping empty ones and rejecting a blanket wildcard.
FAQ
- What does Redact Secrets From Log Lines print when run?
- Run it as a complete program — the output lines carry labels such as `raw :`, `safe:`, `clean:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.