Skip to content

WangWilly/rust-patent-protector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-patent-protector

Toy project to learn Rust and Diesel.

Backend (Rust)

Setup

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install Diesel CLI
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/diesel-rs/diesel/releases/latest/download/diesel_cli-installer.sh | sh

# Create database
cd develop-gears
docker compose up -d

# Database tables
diesel migration generate [migration-name]

# Run migrations
./scripts/migrate.sh

Run

./scripts/serve-backend.sh dev

Deploy

./scripts/build-migration.sh
./scripts/build-backend.sh

cd deployments
docker compose up -d

Resources:

Misc

Learning Rust

To move or to borrow? That is the question

Why This Design? Rust’s Ownership Model

let mut b1 = 1;
let b2 = &mut b1;
let b3 = &mut b1;  // Fail. Cannot mutably borrow when already mutably borrowed
println!("{:?} {:?} {:?}", b1, b2, b3);
  1. First Mutable Borrow: The line let b2 = &mut b1; creates a mutable reference to b1. At this point, b1 is mutably borrowed by b2.
  2. Second Mutable Borrow: The line let b3 = &mut b1; attempts to create another mutable reference to b1. This violates Rust’s borrowing rules because b1 is already mutably borrowed by b2. Rust does not allow multiple mutable borrows at the same time to prevent data races.
  3. Borrow Checker Error: The compiler throws an error because it detects that b1 is being borrowed mutably more than once at the same time, which could lead to undefined behavior if allowed.

Rust’s design aims to prevent data races at compile time by enforcing these borrowing rules. Data races occur when two or more threads access shared data simultaneously, and at least one of the accesses is a write. By ensuring that only one mutable reference exists at any given time, Rust guarantees that no other part of the program can modify the data unexpectedly, thus maintaining memory safety and preventing data races. This approach allows Rust to provide high performance and safety guarantees without needing a garbage collector, making it suitable for systems programming where both efficiency and reliability are critical.

Why using macros?

To illustrate the necessity of using macros in Rust, let's consider a situation where you need to generate repetitive or boilerplate code. Macros provide a powerful way to automate this process, reducing errors and improving maintainability.

Read more

Lifetimes explaination

Lifetimes are a fundamental concept in Rust that help the compiler ensure memory safety without the need for a garbage collector. Understanding lifetimes is crucial for writing safe and efficient Rust code.

Read more

Logging in Rust

Error Handling in Rust

You should use core::result::Result when you need to represent the outcome of an operation that can either succeed or fail. This type is particularly useful in functions that may encounter errors and need to propagate them to the caller.

Read more