{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext rust_magic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### rust_fn command" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following chunk of code" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "code_folding": [ 1 ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "%%rust\n", "fn add(a: i32, b: i32) -> i32 { \n", " a + b \n", "}\n", "add(2, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "can be separated into two cells:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "code_folding": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: ['add']\n" ] } ], "source": [ "%%rust_fn add\n", "fn add(a: i32, b: i32) -> i32 { \n", " a + b \n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "%rust add(2, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Identificator in the first line of the cell must not necessarily match function name. \n", "It is used to avoid multiple definitions of the same function in the final program code: " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "code_folding": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: ['add']\n" ] } ], "source": [ "%%rust_fn add\n", "fn add(a: i32, b: i32) -> i32 { \n", " println!(\"adding\")\n", " a + b \n", "} // add() function updated" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: ['add']\n" ] } ], "source": [ "%%rust_fn add\n", "fn add3(a: i32, b: i32, c: i32) -> i32 {\n", " a + b + c\n", "} // add() function is no longer available " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can even be omitted – subsequent `%rust_fn` without arguments will then remove the previous definitions:" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: ['add', '']\n" ] } ], "source": [ "%%rust_fn\n", "fn f(x: f64) -> f64 { 2*x }" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: ['add', '']\n" ] } ], "source": [ "%%rust_fn\n", "fn f(x: f64) -> f64 { 3*x } // f() updated" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: ['add', '']\n" ] } ], "source": [ "%%rust_fn\n", "fn g(x: f64) -> f64 { x*x } // f() is no longer available" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see a list of the defined functions (of function cell identifiers to be precise) use" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: ['add', '']\n" ] } ], "source": [ "%rust_fn --list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To clear the list of the defined functions use" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Funcs: []\n" ] } ], "source": [ "%rust_fn --clear" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }