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

ob-rustic evaluation with cargo script ? #267

Closed
flintforge opened this issue Aug 9, 2021 · 3 comments · Fixed by #304
Closed

ob-rustic evaluation with cargo script ? #267

flintforge opened this issue Aug 9, 2021 · 3 comments · Fixed by #304

Comments

@flintforge
Copy link

flintforge commented Aug 9, 2021

Hi there.

I'm still a noob with cargo but I would like to suggest a way to evaluate code blocks with cargo script.
Here is why:
The two main use cases I see for org-babel are:

  1. to test code snippets and to provide code examples
  2. in literate programming, when weaving programs using :noweb or similar methods.

As we always need to include or import sections of code but also to isolate fragments for testing purpose, eventually the first case get folded into the second one. But the correctness of a section of code isn't necessarily the same as an entire program, right ? The first example that comes to mind being an unused variable declared in one section, but actually used further (e.g. in an other code block).

My suggestion is that we should have two forms of evaluations:

  • for a complete and correct program or program example: going through the standard build procedure.
  • for a more "relaxed" form, eventually evaluating through cargo script

Before going through a complex tangle process, this would allow partial evaluation of single blocks while preserving the ability to program with :noweb. What do you think ?

@brotzeit
Copy link
Owner

brotzeit commented Aug 24, 2021

:noweb is for tangling like #122 ?

And you like to use cargo script for stuff like:

fn main() {
    let result = {
        #{script}
    };
    println!("{:?}", result);
}

so you can use it like

$ cargo script -t grabbag -e "mem::size_of::<Box<Read>>()"

@brotzeit
Copy link
Owner

Can you maybe provide an example ? I'm not sure I understand.

@flintforge
Copy link
Author

The example from the cargo-script documentation gives the idea, however I had a better experience with older releases of cargo-script. Both the repos and the cargo release looks presently in a bad shape :/

  1. the idea is to evaluate any code statement without a fn main(){} wrapping. This is what PR ob-rustic : run code snippets with no main() #266 does.
    for instance
#+begin_src rust
vec![1, 2, 3].iter().map(|&x| x + 1).collect::<Vec<_>>()
#+end_src

#+RESULTS:
: [2, 3, 4]
  1. The compiler would still complain about unused variables ; That's why I suggest we
  • use cargo-script (or the fork cargo-eval) or
  • add an #[allow(dead_code)] directive in rustic-babel according to some additional predicate
  1. In the weaving process
#+name: variables
#+begin_src rust
// declare variables here
#+end_src
 
#+begin_src rust :noweb yes
<<variables>>
// make use or variables here
#+end_src

local independent evaluations of both code blocks should be correct,
While the weaving and tangling process would discard the <<noweb>> directive, select both code blocks (and more, with any kind of filter and selection through an org mode file — I'm not talking about the regular org-babel-tangle) to compose the final file.

lroolle pushed a commit to lroolle/rustic that referenced this issue Nov 5, 2021
lroolle pushed a commit to lroolle/rustic that referenced this issue Nov 5, 2021
lroolle pushed a commit to lroolle/rustic that referenced this issue Nov 5, 2021
lroolle pushed a commit to lroolle/rustic that referenced this issue Nov 5, 2021
lroolle pushed a commit to lroolle/rustic that referenced this issue Nov 5, 2021
lroolle pushed a commit to lroolle/rustic that referenced this issue Nov 5, 2021
lroolle pushed a commit to lroolle/rustic that referenced this issue Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants