I'm a new comer to Rust but I've been programming other stuff for decades. I've done my "hello world" and now I want to progress to database stuff.
I watched a tutorial on Crate, so I have an idea how that works.
Now I'm trying to follow https://crates.io/crates/postgres-protocol/0.6.4 and postgres - Rust but the example code doesn't work on my machine.
This is my Cargo.toml file. I've added all the dependencies that were listed
[package]
name = "prj_test_db"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
base64 = "0.13.1"
byteorder = "1.4.3"
bytes = "1.4.0"
fallible-iterator = "0.2.0"
hmac = "0.12.1"
md-5 = "0.10.5"
memchr = "2.5.0"
postgres-protocol = "0.6.4"
rand = "0.8.5"
sha2 = "0.10.6"
stringprep = "0.1.2"
This is my main.rs where I put the exact example code from the website inside a main function.
use postgres::{Client, NoTls};
fn main() {
let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
client.batch_execute("
CREATE TABLE person (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
data BYTEA
)
")?;
let name = "Ferris";
let data = None::<&[u8]>;
client.execute(
"INSERT INTO person (name, data) VALUES ($1, $2)",
&[&name, &data],
)?;
for row in client.query("SELECT id, name, data FROM person", &[])? {
let id: i32 = row.get(0);
let name: &str = row.get(1);
let data: Option<&[u8]> = row.get(2);
println!("found person: {} {} {:?}", id, name, data);
}
println!("Hello, world!");
}
And the command line gives me...
matthew@matthew-QEMU:~/Rust/004 PostgreSQL/prj_test_db$ cargo run
Compiling prj_test_db v0.1.0 (/home/matthew/Rust/004 PostgreSQL/prj_test_db)
error[E0432]: unresolved import `postgres`
--> src/main.rs:1:5
|
1 | use postgres::{Client, NoTls};
| ^^^^^^^^ use of undeclared crate or module `postgres`
error[E0308]: mismatched types
--> src/main.rs:18:18
|
18 | &[&name, &data],
| ^^^^^ expected `&str`, found enum `Option`
|
= note: expected reference `&&str`
found reference `&Option<&[u8]>`
Some errors have detailed explanations: E0308, E0432.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `prj_test_db` due to 2 previous errors
I've run and read rustc --explain E0308 and rustc --explain E0432 and I still have no idea what is wrong.
What am I missing?