%load_ext rust_magic
The following chunk of code
%%rust
fn add(a: i32, b: i32) -> i32 {
a + b
}
add(2, 3)
5
can be separated into two cells:
%%rust_fn add
fn add(a: i32, b: i32) -> i32 {
a + b
}
Funcs: ['add']
%rust add(2, 3)
5
Identificator in the first line of the cell must not necessarily match function name.
It is used to avoid multiple definitions of the same function in the final program code:
%%rust_fn add
fn add(a: i32, b: i32) -> i32 {
println!("adding")
a + b
} // add() function updated
Funcs: ['add']
%%rust_fn add
fn add3(a: i32, b: i32, c: i32) -> i32 {
a + b + c
} // add() function is no longer available
Funcs: ['add']
It can even be omitted – subsequent %rust_fn
without arguments will then remove the previous definitions:
%%rust_fn
fn f(x: f64) -> f64 { 2*x }
Funcs: ['add', '']
%%rust_fn
fn f(x: f64) -> f64 { 3*x } // f() updated
Funcs: ['add', '']
%%rust_fn
fn g(x: f64) -> f64 { x*x } // f() is no longer available
Funcs: ['add', '']
To see a list of the defined functions (of function cell identifiers to be precise) use
%rust_fn --list
Funcs: ['add', '']
To clear the list of the defined functions use
%rust_fn --clear
Funcs: []