trait-concept

Rust Newtype wrappers for external traits

Wrap an external type in a tuple struct to create a genuinely distinct local type. You can define formatting or domain behavior for the wrapper without changing the wrapped type. A type alias would not create this distinction. Expected output (not executed): red, blue Why: Tags owns the vector and its Display implementation borrows it. Common mistake: Assuming methods of Vec automatically become methods of Tags.

Formal Syntax & Structure

rust syntaxrust
struct Local(Foreign);

Working Example

Example Snippetrust
use std::fmt;struct Tags(Vec<String>);impl fmt::Display for Tags {    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {        write!(f, "{}", self.0.join(", "))    }}fn main() { println!("{}", Tags(vec!["red".into(), "blue".into()])); }
Rust Reference