使用Rust和Org模式进行文学编程

3
所以我正在尝试在org mode中使用Rust,在安装了ob-rscargo-script之后,一切都很好。但问题是,我无法弄清楚如何从源代码块中使用其他的crates(比如rand)。我尝试将主项目转换为cargo项目,并将所有块解开到主文件中,但这样做实际上就可以使用普通的rust项目了。那么,在org mode块中有没有一种使用外部crates的方法呢?否则,在org mode中使用Rust会变得相当困难,因为许多基本功能都是在外部crates中定义的。
1个回答

3

好的,我发现我们可以使用rustic在org mode中使用外部crate。下面是从主README中进行操作的步骤。

Org-babel

Blocks run asynchronously and a running babel process is indicated by a spinner in the mode-line. It's possible to use crates in babel blocks.

Execute babel block with org-babel-execute-src-block

#+BEGIN_SRC rustic :crates '((regex . 0.2))   extern crate regex;

  use regex::Regex;

  fn main() {
      let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
      assert!(re.is_match("2014-01-01"));   }
#+END_SRC

Supported org babel parameters:

Write to file :results file :file ~/babel-output

Customization:

rustic-babel-format-src-block format block after successful build
rustic-babel-display-compilation-buffer display compilation buffer of babel process
rustic-display-spinner turn off spinner in the mode-line

使用“rustic”作为“src”类型不完美,因为在github渲染org文档时无法进行语法高亮。此外,我们无法在块之间共享功能,但这是一个开放问题,可以在此处找到,因此希望我们能够在某个时间点获得该功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接