How do I get Rust execution in my Quarto blog? #10399
-
DescriptionI'm trying to write an extension so I can have the output of Rust code be rendered into the Quarto document. Here is my Quarto blog post. ---
title: "Executing Rust in Quarto"
author: "Galen Seilis"
date: "2024-07-26"
categories: [Quarto, Rust, Lua, Pandoc]
---
Here is an example of Rust.
```{rust}
fn main() {
// Statements here are executed when the compiled binary is called.
// Print text to the console.
println!("Hello World!");
}
```. The desired output would be the Rust code, then followed below by the literal text "Hello World". Here is my attempt at Lua. I am not familiar with Lua, so do be on the lookout for bugs. -- rust-executor.lua
local io = require("io")
local os = require("os")
local tempfile = require("os").tmpname
-- Helper function to execute Rust code and return the output
local function execute_rust_code(code)
-- Write the Rust code to a temporary file with a .rs extension
local temp_file = tempfile() .. ".rs"
local source_file = io.open(temp_file, "w")
source_file:write(code)
source_file:close()
-- Create a temporary binary file for the compiled executable
local temp_bin = tempfile()
-- Compile the Rust code
local compile_command = "rustc " .. temp_file .. " -o " .. temp_bin
local result = os.execute(compile_command)
if result ~= 0 then
error("Rust compilation failed. Command: " .. compile_command)
end
-- Execute the compiled binary and capture the output
local output = io.popen(temp_bin .. " 2>&1"):read("*a")
-- Clean up temporary files
os.remove(temp_file)
os.remove(temp_bin)
return output
end
-- Lua filter function
function CodeBlock(elem)
-- Check if the block is Rust code
if elem.attr.classes:includes("rust") then
-- Execute the Rust code and get the output
local output = execute_rust_code(elem.text)
-- Remove any trailing newlines or extra whitespace from output
output = output:gsub("%s+$", "")
-- Replace the code block with the output
return pandoc.Para(pandoc.Str(output))
end
end Here is my Quarto version: $ quarto --version
1.5.54 Here is my basic OS info: $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Oh wait, I forgot to capture stdout. |
Beta Was this translation helpful? Give feedback.
-
Here are some hints to follow considering native engine support from Quarto Using knitr engine.In the R Markdown / Knitr ecosytem, it exists some tools allowing to run Rust from R. For example rextendr, which offers a knitr engine which can be used in Quarto documents https://extendr.github.io/rextendr/articles/rmarkdown.html knitr also natively support an Using Jupyter engineIt seems like project Evcxr project (EValuation ConteXt for Rust) offers a Jupyter kernel for Rust. And any kernel can be used with Quarto. About Lua filter for engine executionThose are two more options for running Rust in a Quarto document. Quarto does not offer yet a good engine extention mechanism. The syntax Using a Lua filter to create a new engine could be working (Tools like shinylive leverages this), but it is not that safe for future Quarto (no promises this won't break). Until there are new mechanism to help create external engine, best is to leverage existing one and using tools allowing interoperability. I know this is not the best experience, however this is meant to stay so good for safe usage. Hope this helps |
Beta Was this translation helpful? Give feedback.
Here are some hints to follow considering native engine support from Quarto
Using knitr engine.
In the R Markdown / Knitr ecosytem, it exists some tools allowing to run Rust from R. For example rextendr, which offers a knitr engine which can be used in Quarto documents https://extendr.github.io/rextendr/articles/rmarkdown.html
knitr also natively support an
exec
engine, that can be used to add support for calling any CLI tool for a cell: https://github.com/yihui/knitr-examples/blob/master/124-exec-engine.RmdUsing Jupyter engine
It seems like project Evcxr project (EValuation ConteXt for Rust) offers a Jupyter kernel for Rust. And any kernel can be used with Quarto.
About Lua filter for e…