pub struct SecureString(/* private fields */);Expand description
A secure string that automatically zeros its memory on drop.
This type should be used for any sensitive data like API keys, tokens, or passwords to prevent potential memory disclosure through:
- Core dumps
- Swap files
- Memory scanning tools
- Debuggers
§Security
The contained data is automatically zeroed when the value is dropped,
using the zeroize crate which provides compiler-fence-backed guarantees
that the zeroing operation won’t be optimized away.
§Design: Why No Deref<Target=str>?
This type intentionally does NOT implement Deref to maintain security:
- Explicit access: Requires
.as_ref()call, making code auditable - Prevents silent leakage: No implicit coercion to
&strin logs/errors - Grep-able security: Easy to audit with
git grep "\.as_ref\(\)" - Industry standard: Aligns with
secrecycrate’s proven approach
The slight ergonomic cost of typing .as_ref() is a worthwhile
security trade-off that prevents accidental secret exposure.
§Example
use api_keys_simplified::SecureString;
let sensitive = SecureString::from("my_secret_api_key");
// Explicit access (good - auditable)
let key = sensitive.as_ref();
// Debug output is automatically redacted (safe)
println!("{:?}", sensitive); // Output: "SecureString([REDACTED])"// Memory is automatically zeroed when sensitive goes out of scope
Implementations§
Trait Implementations§
Source§impl AsRef<str> for SecureString
impl AsRef<str> for SecureString
Source§impl Clone for SecureString
impl Clone for SecureString
Source§fn clone(&self) -> SecureString
fn clone(&self) -> SecureString
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SecureString
impl Debug for SecureString
Source§impl Display for SecureString
impl Display for SecureString
Source§impl Drop for SecureString
impl Drop for SecureString
Source§impl From<&str> for SecureString
impl From<&str> for SecureString
Source§impl From<String> for SecureString
impl From<String> for SecureString
Auto Trait Implementations§
impl Freeze for SecureString
impl RefUnwindSafe for SecureString
impl Send for SecureString
impl Sync for SecureString
impl Unpin for SecureString
impl UnwindSafe for SecureString
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more