SecureString

Struct SecureString 

Source
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 &str in logs/errors
  • Grep-able security: Easy to audit with git grep "\.as_ref\(\)"
  • Industry standard: Aligns with secrecy crate’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§

Source§

impl SecureString

Source

pub fn new(s: String) -> Self

Creates a new SecureString from a String.

The original string is moved and will be zeroed when this SecureString is dropped.

Source

pub fn len(&self) -> usize

Returns the length of the string in bytes.

Source

pub fn is_empty(&self) -> bool

Returns true if the string is empty.

Trait Implementations§

Source§

impl AsRef<str> for SecureString

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for SecureString

Source§

fn clone(&self) -> SecureString

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SecureString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SecureString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for SecureString

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<&str> for SecureString

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for SecureString

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl Zeroize for SecureString

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.