> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rust Unsafe Trait

An unsafe trait is a trait containing specific semantic invariants that the Rust compiler cannot automatically verify. By marking a trait with the `unsafe` keyword, the author dictates that any implementation of this trait must also be explicitly marked as `unsafe`. This transfers the responsibility of upholding the trait's compiler-invisible contracts from the compiler to the implementer.

## Syntax and Declaration

To define an unsafe trait, the `unsafe` keyword precedes the `trait` declaration:

```rust theme={"dark"}
unsafe trait ValidatesInvariants {
    fn perform_action(&self);
}
```

To implement this trait, the implementer must use an `unsafe impl` block. This acts as an explicit acknowledgement by the developer that they have read the trait's documentation and guarantee that the type fulfills all required invariants.

```rust theme={"dark"}
struct MyType;

// The implementer assumes responsibility for the trait's invariants
unsafe impl ValidatesInvariants for MyType {
    fn perform_action(&self) {
        // Method implementation
    }
}
```

## Orthogonality of Unsafe Traits and Unsafe Methods

A critical mechanical distinction in Rust is the separation between unsafe traits and unsafe methods. The `unsafe` keyword applies strictly to the boundary it modifies:

1. **Unsafe Trait (`unsafe trait`)**: The danger lies in *implementing* the trait incorrectly. Calling the methods on the trait is entirely safe, provided the implementer upheld the contract.
2. **Unsafe Method (`unsafe fn`)**: The danger lies in *calling* the method. Implementing the trait is safe, but the caller must uphold specific preconditions before invoking the function.

```rust theme={"dark"}
// 1. Unsafe to implement, safe to call
unsafe trait TrustMe {
    fn safe_to_call(&self); 
}

// 2. Safe to implement, unsafe to call
trait BeCareful {
    unsafe fn unsafe_to_call(&self);
}

// 3. Unsafe to implement, unsafe to call
unsafe trait ExtremeCaution {
    unsafe fn unsafe_to_call(&self);
}
```

## Auto Traits and Unsafe

In Rust's type system, marker traits like `Send` and `Sync` are implemented as `unsafe auto trait`. The compiler automatically implements these traits for types composed entirely of other `Send`/`Sync` types.

However, if a developer needs to manually implement an auto trait for a type that the compiler rejects (e.g., a struct containing raw pointers), they must bypass the compiler's safety checks using an `unsafe impl`.

```rust theme={"dark"}
struct RawWrapper {
    ptr: *mut u8,
}

// The compiler will not auto-implement Send because of the raw pointer.
// The developer must use `unsafe impl` to assert thread-safety invariants manually.
unsafe impl Send for RawWrapper {}
```

## Supertraits and Unsafe

If a safe trait relies on an unsafe trait as a supertrait, implementing the safe trait does not require the `unsafe` keyword. However, the type must still provide an `unsafe impl` for the underlying unsafe supertrait.

```rust theme={"dark"}
unsafe trait BaseUnsafe {}

// Safe trait with an unsafe supertrait
trait DerivedSafe: BaseUnsafe {}

struct Data;

// Requires unsafe
unsafe impl BaseUnsafe for Data {}

// Does not require unsafe
impl DerivedSafe for Data {}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
