Coder Social home page Coder Social logo

ob-rust's Introduction

ob-rust https://travis-ci.org/micanzhang/ob-rust.svg?branch=master

Introduction

ob-rust enables Org-Babel support for evaluating Rust code. It was created based on the usage of ob-C and ob-go.

#+begin_src rust
fn main() {
    let greetings = ["Hello", "Hola", "Bonjour",
                     "Ciao", "こんにちは", "안녕하세요",
                     "Cześć", "Olá", "Здравствуйте",
                     "chào bạn", "您好"];

    for (num, greeting) in greetings.iter().enumerate() {
        print!("{} : ", greeting);
        match num {
            0 =>  println!("This code is editable and runnable!"),
            1 =>  println!("Este código es editable y ejecutable!"),
            2 =>  println!("Ce code est modifiable et exécutable!"),
            3 =>  println!("Questo codice è modificabile ed eseguibile!"),
            4 =>  println!("このコードは編集して実行出来ます!"),
            5 =>  println!("여기에서 코드를 수정하고 실행할 수 있습니다!"),
            6 =>  println!("Ten kod można edytować oraz uruchomić!"),
            7 =>  println!("Esse código é editável e executável!"),
            8 =>  println!("Этот код можно отредактировать и запустить!"),
            9 =>  println!("Bạn có thể edit và run code trực tiếp!"),
            10 =>  println!("这段代码是可以编辑并且能够运行的!"),
            _ =>  {},
        }
    }
}
#+end_src

:

#+resutls:
Hello : This code is editable and runnable!
Hola : Este código es editable y ejecutable!
Bonjour : Ce code est modifiable et exécutable!
Ciao : Questo codice è modificabile ed eseguibile!
こんにちは : このコードは編集して実行出来ます!
안녕하세요 : 여기에서 코드를 수정하고 실행할 수 있습니다!
Cześć : Ten kod można edytować oraz uruchomić!
Olá : Esse código é editável e executável!
Здравствуйте : Этот код можно отредактировать и запустить!
chào bạn : Bạn có thể edit và run code trực tiếp!
您好 : 这段代码是可以编辑并且能够运行的!

Requirements

rust and cargo

To install rust, run the following in your terminal:

curl https://sh.rustup.rs -sSf | sh

for more details or other methods installation, please visit: https://www.rust-lang.org/en-US/install.html

cargo script

You can install rust-script using Cargo’s install subcommand:

cargo install rust-script

TODOs

  • replace rust-script with rustc and run binary.

ob-rust's People

Contributors

crosiek avatar micanzhang avatar pismute avatar svend avatar xuchunyang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ob-rust's Issues

using extern crate

Out of the box :
C-c C-c

Compiling rust-src-...
error: use of unstable library feature 'rustc_private': 
this crate is being loaded from the sysroot, an unstable location; 
did you mean to load this crate from crates.io via `Cargo.toml` instead? (see issue #27812)

Workaround : use nightly build
curl -s https://static.rust-lang.org/rustup.sh | sh -s -- --channel=nightly
and add

#![feature(rustc_private)]

in the source header

Babel "functional mode" and argument passing?

Is it (or could it be) possible to pass arguments to Rust snippets from previous source blocks, as in this example:

#+name: directories
#+begin_src sh
cd ~/sync/org && du -sc * | grep -v total
#+end_src

#+RESULTS: directories
|   8 | 2019.org     |
| 156 | 2020.org     |
|  28 | 2021.org     |
|  68 | coding.org   |
|   4 | history.txt  |
|   4 | notes.org    |
|   4 | sysadmin.org |

#+begin_src python :var dirs=directories
return len(dirs)
#+end_src

#+RESULTS:
: 7

Thank you.

Travis CI tests fail

Although the Travis CI badge is green, the tests are actually failing, see logs at https://travis-ci.org/micanzhang/ob-rust.

I suggest deleting all the test code because it is not working, because

  • You forgot to install Rust and cargo-script
  • org-babel-rust-command is not defined.

Using :args to pass command line arguments to the main function

I have this org file:

:PROPERTIES:
:header-args:rust: :noweb yes :comments noweb
:END:

* The program
** General structure

#+name: main
#+begin_src rust :tangle main.rs
<<imports>>
fn main() {
    <<gather-input>>
    <<count>>
}
<<tests>>
#+end_src

#+RESULTS: main
: thread 'main' panicked at 'No argument provided', src/main.rs:9:9

** Gathering input

We simply get the first parameter, panicking if there is nothing specified.

#+name: gather-input
#+begin_src rust
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
    panic!("No argument provided");
}
<<parse-input>>
#+end_src

... skipped ... 

I can run org-babel-tangle to produce a main.rs file, and things work fine when I run cargo run 3, for example.

However, I would like to provide a command line arguments to the source block, so that I can run org-babel-execute-maybe inside this block, and use a value:

#+name: main
#+begin_src rust :tangle main.rs
<<imports>>
fn main() {
    <<gather-input>>
    <<count>>
}
<<tests>>
#+end_src

I understand that I could use a default argument, etc... However, the doc in ob-rust.el says that args should work, but I could not make it work. Is there something else to do than that ?

#+name: main
#+begin_src` rust :tangle main.rs :args 3
...

No org-babel-execute function for rust!

I installed ob-rust in doom emacs.

Attempting to execute the following fails with "No org-babel-execute function for rust!".

#+begin_src rust
println!("hello");
#+end_src

I have installed rust with rustup, as per the README and attempted to set an org-babel-execute:rust variable manually but had no success.

rust-script runs in the shell and .cargo/bin is in the exec-path and used successfull in term, shell, and eshell.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.