expression
Return an owned peripheral from an abstraction
A HAL wrapper can consume a raw peripheral token and expose a safer interface. Consuming the wrapper to recover its token avoids leaving two live owners. This no_std example uses ordinary in-memory state, not a PAC or hardware pin. A real release API must specify hardware configuration and active transfers. Expected host stdout (not executed): recovered level: true Common mistake: Returning a second owned peripheral while leaving the first wrapper usable.
Formal Syntax & Structure
rust syntaxrust
fn free(self) -> RawPeripheralWorking Example
Example Snippetrust
#![no_std]pub struct RawPin { level: bool }impl RawPin { pub fn simulated() -> Self { Self { level: false } } }pub struct Output(RawPin);impl Output { pub fn new(raw: RawPin) -> Self { Self(raw) } pub fn high(&mut self) { self.0.level = true; } pub fn free(self) -> RawPin { self.0 }}impl RawPin { pub fn level(&self) -> bool { self.level } }