NeonN
Neon14mo ago
1 reply
urgent-maroon

Rust Up & Running Tutorial not compiling

Following: https://neon.tech/docs/guides/rust
And I'm getting this error:

matt@laptop: ~/RustroverProjects/seaorm on HEAD (unknown) [?]
$ cargo run
   Compiling seaorm v0.1.0 (/Users/matthewharwood/RustroverProjects/seaorm)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s
     Running `target/debug/seaorm`
thread 'main' panicked at /Users/matt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/postgres-0.19.9/src/config.rs:449:44:
Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


use std::error;
use axum::routing::get;
use axum::Router;
use std::net::SocketAddr;
use openssl::ssl::{SslConnector, SslMethod};
use postgres::Client;
use postgres_openssl::MakeTlsConnector;

async fn db() -> Result<(), Box<dyn error::Error>>  {
    let builder = SslConnector::builder(SslMethod::tls())?;
    let connector = MakeTlsConnector::new(builder.build());
    let mut client = Client::connect("postgresql://neondb_owner:..../neondb?sslmode=require", connector)?;
    for row in client.query("SELECT 42", &[])? {
        let ret : i32 = row.get(0);
        println!("Result = {}", ret);
    }
    Ok(())
}
#[tokio::main]
async fn main() {
    let app = Router::new().route("/hello", get(|| async { "Hello, world!" }));
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    db().await.unwrap();

    println!("listening on {}", addr);

    axum::serve(tokio::net::TcpListener::bind(&addr).await.unwrap(), app)
        .await
        .unwrap();

}

Cargo File:

[package]
name = "seaorm"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = "0.8.0-alpha.1"
openssl = "0.10.68"
postgres = "0.19.9"
postgres-openssl = "0.5.0"
tokio = { version = "1.41.1", features = ["full"] }
Was this page helpful?