Jupyter for Rust¶
Rust的evcxr_jupyter
包提供了对jupyter notebook的支持,可以在notebook中运行Rust代码。使用如下命令安装:
cargo install --locked evcxr_jupyter
evcxr_jupyter --install
另外,还需要安装Rust标准库的源代码:
rustup component add rust-src
之后,就可以在notebook中运行Rust代码了。
jupyter notebook
In [2]:
Copied!
fn add(a: i32, b: i32) -> i32 {
a + b
}
add(1, 2) // The last expression is evaluated and printed
fn add(a: i32, b: i32) -> i32 {
a + b
}
add(1, 2) // The last expression is evaluated and printed
Out[2]:
3
使用:dep <package>
来导入包。evcxr_input
包提供了get_string
和get_password
函数,可以在notebook中输入字符串和密码。
In [3]:
Copied!
:dep evcxr_input
let name = evcxr_input::get_string("Name?");
let password = evcxr_input::get_password("Password?");
println!("Hello, {name}! Your password is {password}", name=name, password=password);
:dep evcxr_input
let name = evcxr_input::get_string("Name?");
let password = evcxr_input::get_password("Password?");
println!("Hello, {name}! Your password is {password}", name=name, password=password);
Hello, Alice! Your password is alice123
使用如下命令卸载Rust
evcxr_jupyter --uninstall
cargo uninstall evcxr_jupyter