Setting up Rust environment errors

Hi,

I am trying the most basic Rust example to interact with the network here. sui/crates/sui-sdk/examples/get_owned_objects.rs at main · MystenLabs/sui · GitHub

I have added:

[dependencies]
sui-sdk = { git = "https://github.com/MystenLabs/sui" }

to Cargo.toml. Installed LLVM, protoc.exe. When I run Cargo build I get the following errors:

PS D:\Rust Projects> cargo build
error: could not find Cargo.toml in D:\Rust Projects or any parent directory
PS D:\Rust Projects> cd my_test
PS D:\Rust Projects\my_test> cargo build
Compiling my_test v0.1.0 (D:\Rust Projects\my_test)
error[E0432]: unresolved import sui_json_rpc_types
→ src\main.rs:5:5
|
5 | use sui_json_rpc_types::{SuiObjectDataOptions, SuiObjectResponseQuery};
| ^^^^^^^^^^^^^^^^^^ use of undeclared crate or module sui_json_rpc_types

error[E0433]: failed to resolve: use of undeclared crate or module tokio
→ src\main.rs:9:3
|
9 | #[tokio::main]
| ^^^^^ use of undeclared crate or module tokio

error[E0433]: failed to resolve: use of undeclared crate or module anyhow
→ src\main.rs:10:31
|
10 | async fn main() → Result<(), anyhow::Error> {
| ^^^^^^ use of undeclared crate or module anyhow

error[E0752]: main function is not allowed to be async
→ src\main.rs:10:1
|
10 | async fn main() → Result<(), anyhow::Error> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ main function is not allowed to be async

Some errors have detailed explanations: E0432, E0433, E0752.
For more information about an error, try rustc --explain E0432.
error: could not compile my_test (bin “my_test”) due to 4 previous errors
PS D:\Rust Projects\my_test>

I am using Windows 10 and VS Code. I would be very grateful if someone knows what they mean and what I need to do. Many thanks in advance.

7 Likes

I have fixed three of the errors by adding two more lines to the dependencies section of Cargo.toml.

tokio = { version = "1", features = ["full"] }
anyhow = "1

I still have the following error.

use sui_json_rpc_types::{SuiObjectDataOptions, SuiObjectResponseQuery};
  |     ^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `sui_json_rpc_types`

I can see the sui_json_rpc_types crate in the list of crates with all the rest here: Index of crates

Anyone who can tell me what I am doing wrong will be placed forever in a special place in my heart. Thanks.

1 Like

I’m stuck at exactly the same point.

Did you manage to get the example working?

I did but I can’t remember what it was and I uninstalled and deleted everything. I got it to compile and run and got some unfathomable error at runtime. Couldn’t get help on discord, nothing on Google. Every single example I tried had multiple errors which is why I gave up. I am working on my game and I will come back or try another network in a year or so when the technology and the user base has matured and grown. The best Rust help I found was chat gpt although it knows nothing of Sui speciffically, it solved some rust issues. Sorry I don’t have the answer. I did get some smart contracts published via the web based compiler/deployer but interacting with them or creating a wallet completley eluded me.

1 Like

Thanks for the reply!

Very frustrating that the basic examples don’t work…

I finally got the basic “get objects owned by an address” working, adding here in case others stumble upon this post:

use serde_json::{json, Value};
use std::str::FromStr;
use sui_sdk::rpc_types::{Page, SuiObjectDataOptions, SuiObjectResponse, SuiObjectResponseQuery};
use sui_sdk::types::base_types::{ObjectID, SuiAddress};
use sui_sdk::SuiClientBuilder;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    let address_string = "0xe85738f9cb22cf80f77b861a9e9aca13c0a297cf0f1c075d35a9b3fb94bf6f89";
    let sui = SuiClientBuilder::default()
        .build("https://fullnode.mainnet.sui.io:443")
        .await?;
    let address = SuiAddress::from_str(address_string)?;
    let objects = sui
        .read_api()
        .get_owned_objects(
            address,
            Some(SuiObjectResponseQuery::new_with_options(
                SuiObjectDataOptions::full_content(),
            )),
            None,
            None,
        )
        .await?;
    let _ = pretty_print_objects(objects);
    Ok(())
}

fn pretty_print_objects(objects: Page<SuiObjectResponse, ObjectID>) -> Result<(), anyhow::Error> {
    let mut json_array: Vec<Value> = Vec::new();
    for object in objects.data {
        let value = object.data.unwrap();
        let json_object: Value = json!({
            "id": value.object_id,
            "digest": value.digest,
        });
        json_array.push(json_object);
    }
    let json_string = serde_json::to_string(&json_array).unwrap();
    println!("{}", json_string);
    Ok(())
}