liveBook · Manning

Trait

As explained in Rust Servers, Services, and Apps

A trait is a Rust abstraction that defines shared behavior or type constraints independently of a concrete type.

A trait defines shared behavior or type constraints independently of a concrete type. In a basic Rust web server, traits help model behavior around real-world network protocols and extend application data structures. section 2.0

The web-server implementation uses traits to:

  • Support a library that converts TCP byte streams into HTTP messages and models requests and responses with Rust structs and enums. section 2.0
  • Add behavior to custom structs through both automatically derived traits and hand-coded trait implementations. section 2.4
  • Strengthen practical Rust understanding while building a server for static web pages and JSON data. section 2.4

Traits in the basic web server

A standard-library-only Rust project builds a TCP server, an HTTP parsing and response library, and an HTTP server that responds to browsers and curl. Traits participate in giving the custom protocol structs additional behavior, with some implementations generated through Rust annotations and others written explicitly. section 2.0 section 2.4

How Rust Servers, Services, and Apps covers it

How deep it goes
Demonstrates derived and hand-coded implementations on custom structs, without detailing trait internals.
Examples
Uses a TCP client-server exchange, an HTTP library, and a server for static pages and JSON data.
What you can do after
Apply traits to model protocol behavior and add derived or custom behavior to Rust data structures.
How it connects
Connects traits with structs, enums, lifetimes, HTTP, TCP, and derivable traits.

Also explained in

Compare how 4 books explain trait →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.