Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getting started code example #875

Closed
ratzupaltuff opened this issue Jul 24, 2022 · 5 comments
Closed

Improve getting started code example #875

ratzupaltuff opened this issue Jul 24, 2022 · 5 comments
Labels
needs-info Further information is requested

Comments

@ratzupaltuff
Copy link

Describe the bug
When I copy paste the "Getting started" Code to a fresh rust project it does not compile.
Other Crates have documentation what modules need to be imported in order to use the example, this one does not.
The compiler says use std::convert::TryFrom; is a unnecessary Import.

To Reproduce
Go to https://matrix-org.github.io/matrix-rust-sdk/matrix_sdk/index.html and copy the code.
Create a new Project with cargo new xyz
Paste Code to main.rs
Run cargo run

My Cargo.toml:

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

[dependencies]
anyhow = "1.0"
tokio = { version = "1.20.0", features = ["full"] }
#matrix-sdk = "0.5"

[dependencies.matrix-sdk]
version = "0.5.0"
#default-features = true

My src/main.rs

//use std::convert::TryFrom;
use matrix_sdk::{
    Client, config::SyncSettings,
    ruma::{user_id, events::room::message::SyncRoomMessageEvent},
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let user = user_id!("@test:matrix.org");
    let client = Client::builder().user_id(user).build().await?;

    // First we need to log in.
    client.login_username(user, "password").send().await?;

    client
        .register_event_handler(|ev: SyncRoomMessageEvent| async move {
            println!("Received a message {:?}", ev);
        })
        .await;

    // Syncing is important to synchronize the client state with the server.
    // This method will never return.
    client.sync(SyncSettings::default()).await;

    Ok(())
}

Expected behavior
Clear documentation on necessary Imports and a working example without unused imports.

Desktop (please complete the following information):

  • OS: Archlinux

Additional context
I am new to Rust and this SDK so maybe there are simple answers. Please be kind in your answers.
I opened this Issue to give you a perspective what would be helpful for me as a beginner.

@jplatte
Copy link
Collaborator

jplatte commented Jul 24, 2022

You linked to the SDK's latest git main documentation, but the dependency is on the 0.5 crates.io release. Please refer to its docs at docs.rs instead, or use a git dependency.

@ratzupaltuff
Copy link
Author

Thank you for your answer! You are right, there is a version mismatch.
I looked at the example at https://docs.rs/matrix-sdk/0.5.0/matrix_sdk/index.html and the Issue that the code does not compile is solved.
The other two persist:

  • I would find it helpful for the example if we could mention which dependencies are needed
  • I still get the warning that use std::convert::TryFrom; is a unused dependency

@jplatte
Copy link
Collaborator

jplatte commented Jul 25, 2022

Yeah TryFrom no longer has to be imported explicitly as off Rust 2021. A PR removing it from anywhere it's still imported explicitly would be welcome (same for TryInto).

I would find it helpful for the example if we could mention which dependencies are needed

I've been meaning to move the examples out of crates/matrix-sdk (same as in Ruma and other projects), but it's not a high priority, sorry. A PR for this would also be welcome.

@gnunicorn gnunicorn added the needs-info Further information is requested label Jul 27, 2022
@gnunicorn
Copy link
Contributor

closing as the main issues has been addressed and solved.

@jplatte
Copy link
Collaborator

jplatte commented Jul 27, 2022

Created a new issue about making the examples their own crates: #880

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants