I'm wondering if I can connect from Rust to Memgraph Cloud. If it is important, I'm using a free version of the Memgraph Cloud at the moment.
To connect to a running Memgraph Cloud instance with Rust, you can use the provided code at the Cloud website. Also, the instructions are provided at the Memgraph Cloud documentation.
Here are the steps:
Cargo.toml
file:rsmgclient = "0.1.1"
use rsmgclient::{ConnectParams, Connection, SSLMode};
fn main(){
// Parameters for connecting to database.
let connect_params = ConnectParams {
host: Some(String::from("MEMGRAPH_HOST_ADDRESS")),
port: 7687,
username: Some(String::from("YOUR_MEMGRAPH_USERNAME")),
password: Some(String::from("YOUR_MEMGRAPH_PASSWORD")),
sslmode: SSLMode::Require,
..Default::default()
};
// Make a connection to the database.
let mut connection = match Connection::connect(&connect_params) {
Ok(c) => c,
Err(err) => panic!("Connect failed: {}", err)
};
// Execute a query.
let query = "CREATE (n:FirstNode {message: 'Hello Memgraph from Rust!'}) RETURN id(n) AS nodeId, n.message AS message";
match connection.execute(query, None) {
Ok(c) => c,
Err(err) => panic!("Query failed: {}", err)
};
match connection.fetchall() {
Ok(records) => {
println!("Created node: {}", &records[0].values[1])
},
Err(err) => panic!("{}", err)
};
}