Rust Diesel Single File MySQL Example

use self::schema::widgets;
use self::schema::widgets::dsl::*;
use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;

mod schema;

fn main() {
    println!("Hello world");
    let conn = &mut establish_connection();

    let new_widget = NewWidget {
        content: Some("quick brown fox"),

        .expect("Error inserting new post");

    let results = widgets.load::<Widget>(conn).expect("Error loading posts");
    for widget in results {
        println!("{} {}",, widget.content.unwrap());

pub fn establish_connection() -> MysqlConnection {
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
        .unwrap_or_else(|_| panic!("Error connecting to database"))

pub struct Widget {
    pub id: String,
    pub content: Option<String>,

#[diesel(table_name = widgets)]
pub struct NewWidget<'a> {
    pub content: Option<&'a str>,