liveBook · Manning

Option

As explained in Rust Servers, Services, and Apps

Option is a Rust standard-library generic type representing either a value or its absence.

Option<T> represents a value that may be present or absent. In the course API, it models fields that database records or client payloads do not have to provide. section 6.2.1

Use optional fields when different application boundaries impose different requirements:

  • Course stores optional description, format, structure, duration, price, language, level, and posted time values. section 6.2.1
  • CreateCourse keeps client-supplied fields optional while requiring tutor_id and course_name. section 6.2.1
  • UpdateCourse makes every editable course field optional, allowing partial updates. section 6.2.1
  • Database columns corresponding to optional fields omit NOT NULL, preserving the same absence model. section 6.2.1 section 6.4

Course model

A course can require identity fields while permitting absent metadata: course_id, tutor_id, and course_name are mandatory, whereas course_description and course_price use Option<T>. Serialization and sqlx::FromRow then support API output and database-record conversion. section 6.2.1

#[derive(Serialize, Debug, Clone, sqlx::FromRow)]
pub struct Course {
    pub course_id: i32,
    pub tutor_id: i32,
    pub course_name: String,
    pub course_description: Option<String>,
    pub course_format: Option<String>,
    pub course_structure: Option<String>,
    pub course_duration: Option<String>,
    pub course_price: Option<i32>,
    pub course_language: Option<String>,
    pub course_level: Option<String>,
    pub posted_time: Option<NaiveDateTime>,
}

Inspect the complete Course struct and its Option<T> fields alongside the required fields. section 6.2.1

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains optional struct fields and nullable database columns, but does not develop Option’s broader API or internals.
Examples
Uses Course, CreateCourse, UpdateCourse, and nullable course-table columns to demonstrate optional values.
What you can do after
Apply it via Rust data models that distinguish required fields from values accepted as absent.
How it connects
Connects Rust structs with database schemas, serialization, deserialization, and CRUD APIs.

Also explained in

Compare how 9 books explain option →

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.