Ask coding questions

← Back to all posts
Rust import packages on repl?
h
CaptainAnon (149)

I'm currently reading up on rust, and trying to run through some examples on repl.it. I have, however, yet to figure out how to install packages. Does anyone know how to import rust packages (crates) into repl.it.

Answered by jfrank11 (7) [earned 5 cycles]
View Answer
Comments
hotnewtop
jfrank11 (7)

It looks like they are making improvements or it has been possible ever since who knows. But here is how I managed (you might need a paid account for this would be nice if someone with a free account could check):
Open a shell (question mark on the lower right or (Shift-Cmd/Ctrl-S)

> cargo init helloworld
> cd helloworld
> vi Cargo.toml
put `markdown = "0.2"` under `[dependencies]`
<esc>:x
> vi src/main.rs

enter the following:

extern crate markdown;

fn main() {
    let html : String = markdown::to_html("__I am markdown__");

    println!("{:?}", html);
}
<esc>:x
> cargo run

[email protected]:~/Hello-World/helloworld$ cargo run
   Compiling helloworld v0.1.0 (/home/runner/Hello-World/helloworld)
    Finished dev [unoptimized + debuginfo] target(s) in 3.20s
     Running `target/debug/helloworld`
"<p><strong>I am markdown</strong></p>\n"

Voilà!

joojeene (0)

@jfrank11 that's really useful!
Worked for me except for the
cargo init projectname
where I got this:

> cargo init projectname
error: Failed to create package `projectname` at `
/home/runner/...`

Caused by:
  could not determine the current user, please set $USER

So I had to experiment and had success with:
> USER=runner cargo init projectname

CaptainAnon (149)

@jfrank11 Thanks. That does the trick.

TejasUmesh (0)

found a better method;
just type

cargo init

into your shell and everytime you wanna run your code type

cargo run

and you're gold!

AndrewBota (1)
  1. Make a new Repl Rust project
  2. In the terminal, type bash
  3. Type cargo new {project name}
  4. You should now see a new folder, navigate to the src file inside it and find the main.rs file. DO NOT CLICK RUN TO RUN YOUR PROGRAM!!! IT WILL ONLY RUN THE ORIGINAL main.rs FILE!
  5. To test run your new Cargo project, type cd {project name} in the terminal, and use command cargo run.
  6. If the run succeeds, open Cargo.toml and add
rand = "{latest version of rand}"
  1. Add use rand::Rng in main.rs
  2. You're good to go!
marco_p_v_vezzoli (0)

This would be really useful. In order to write a program which is not a toy I definitely need this