liveBook · Manning

Type safety

As explained in Rust Servers, Services, and Apps

Type safety is the property that program operations respect the constraints of their declared or inferred types.

Rust checks type constraints at compile time. A variable’s type must be declared or inferred; if inference fails or types conflict, compilation stops with a diagnostic. This prevents a class of runtime errors. section 1.2.2

Type safety is one part of program safety, distinct from memory safety and thread safety. In a changing web service, strong typing and compiler checks help make API and data-model refactoring safer. section 1.2.2 section 1.3.1

The compile-time checks extend into database work through SQLx. It checks raw SQL queries against PostgreSQL tables and columns, so a missing table, missing column, or schema mismatch can stop compilation before an API runs. section 4.2.1 section 6.3.4

Add the schema before compiling the API

When adding tutor APIs, create the tutor table in `src/iter5/dbscripts/tutor-course.sql` before compiling the application logic. The script defines columns such as `tutor_id` and `tutor_name`; SQLx checks that the SQL statements match the resulting table description. section 6.3.4

/* Drop tables if they already exist*/
 
drop table if exists ezy_course_c6 cascade;                         #1
drop table if exists ezy_tutor_c6;
 
/* Create tables. */
 
create table ezy_tutor_c6 (                                         #2
    tutor_id serial primary key,
    tutor_name varchar(200) not null,
    tutor_pic_url varchar(200) not null,
    tutor_profile varchar(2000) not null
);
 
create table ezy_course_c6                                          #3
(
    course_id serial primary key,
    tutor_id INT not null,
    course_name varchar(140) not null,
    course_description varchar(2000),
    course_format varchar(30),
    course_structure varchar(200),
    course_duration varchar(30),
    course_price INT,
    course_language varchar(30),
    course_level varchar(30),
    posted_time TIMESTAMP default now(),
    CONSTRAINT fk_tutor                                             #4
    FOREIGN KEY(tutor_id)                                           #4
        REFERENCES ezy_tutor_c6(tutor_id)                           #4
    ON DELETE cascade
);
 
grant all privileges on table ezy_tutor_c6 to <username>;           #5
grant all privileges on table ezy_course_c6 to <username>;
grant all privileges on all sequences in schema public to <username>;;
 
/* Load seed data for testing */                                    #6
insert into ezy_tutor_c6(tutor_id, tutor_name, tutor_pic_url,tutor_profile)
values(1,'Merlene','http://s3.amazon.aws.com/pic1',
'Merlene is an experienced finance professional');
 
insert into ezy_tutor_c6(tutor_id, tutor_name, tutor_pic_url,tutor_profile)
values(2,'Frank','http://s3.amazon.aws.com/pic2',
'Frank is an expert nuclear engineer');
 
insert into ezy_course_c6
    (course_id,tutor_id, course_name,course_level, posted_time)
values(1, 1, 'First course', 'Beginner' , '2021-04-12 05:40:00');
insert into ezy_course_c6
    (course_id, tutor_id, course_name, course_format, posted_time)
values(2, 1, 'Second course', 'ebook', '2021-04-12 05:45:00');

The SQL script shows the tutor and course table definitions that must exist for compile-time query checks to pass. section 6.3.4

How Rust Servers, Services, and Apps covers it

How deep it goes
Defines Rust’s compile-time type checking and inference, then applies compile-time checking to SQL queries without detailing compiler internals.
Examples
Uses tutor API changes and a PostgreSQL script defining tutor and course tables.
What you can do after
Diagnose/debug with it by resolving compiler-reported type conflicts and SQLx-reported schema mismatches before running the service.
How it connects
Connects type inference and compiler diagnostics with SQL query checking, database schemas, and refactoring.

Also explained in

Compare how 8 books explain type safety →

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.