diff --git a/.gitignore b/.gitignore index 7585238..bfa62eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ book +src/examples \ No newline at end of file diff --git a/NOTES.md b/NOTES.md deleted file mode 100644 index 07c4e6c..0000000 --- a/NOTES.md +++ /dev/null @@ -1,4 +0,0 @@ -# Hard-Coded Modifications - -1. [move_assignment](src/assets/modified_examples/move_assignment) - - added "the resource previously owned is dropped" to message on line 239 \ No newline at end of file diff --git a/README.md b/README.md index dfd58b3..c5cfc02 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,11 @@ # RustViz Tutorial This repo contains an introductory Rust tutorial which contains visualizations -generated with [RustViz](https://github.com/fplab/rustviz). It is intended to be +generated with [RustViz](https://github.com/rustviz/rustviz2). It is intended to be used in the Rust unit in EECS 490 at the University of Michigan. ## Usage -### Copying Code Examples Using `copy_assets.sh` -To easily copy over code examples from the rustviz repo, you can run -`copy_assets.sh`. This will copy the source code and SVGs from the -`svg_generator/examples` directory in the rustviz repo to the -`src/assets/code_examples` directory in this repo. Some things to note: -- The script should only be run from the `rustviz-tutorial` directory. -- `rustviz-tutorial` and `rustviz` must have the same parent directory for the -script to work. -- The script will overwrite any existing files with the same name. -- This script doesn't actually run the svg_generator. It simply copies existing -files. You should run the svg generator examples first. - -### Building the Book 1. Install mdbook using `cargo install mdbook` -2. Navigate to the `rustviz-tutorial` directory and run `mdbook build` -3. Navigate to the `rustviz-tutorial/book` directory and run -`python3 -m http.server`. You should be able to view the tutorial in your -browser at http://localhost:8000/ \ No newline at end of file +2. Install the RustViz preprocessor with `rustup run nightly-2024-05-20 cargo install mdbook-rustviz` +3. Run `mdbook serve` to serve an instance of the book +4. For information on how to add your own Rust examples to the book check out the mdbook-rustviz [documentation](https://github.com/rustviz/rustviz2/tree/main/mdbook-rustviz) \ No newline at end of file diff --git a/book.toml b/book.toml index 096b2ec..f5a7c34 100644 --- a/book.toml +++ b/book.toml @@ -5,6 +5,4 @@ multilingual = false src = "src" title = "Tutorial" -[output.html] -additional-css = ["visualization.css"] -additional-js = ["helpers.js"] \ No newline at end of file +[preprocessor.rustviz] \ No newline at end of file diff --git a/build_tutorial.sh b/build_tutorial.sh deleted file mode 100755 index 28450b9..0000000 --- a/build_tutorial.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/bin/bash -red=$'\e[1;31m' -end=$'\e[0m' - -# Write the first line of SUMMARY.md. This clears anything that was there previously -# printf "# Summary\n\n" > src/SUMMARY.md - -printf "Generating visualizations for the following examples: \n" - -# Uncomment the examples are being tested -declare -a targetExamples=( - # "copy" - # "func_take_ownership" - # "func_take_return_ownership" - # "function" - # "hatra1" - # "hatra1_test" - # "hatra2" - # "immutable_borrow" - # "immutable_borrow_method_call" - # "immutable_variable" - # "move_assignment" - # "move_different_scope" - # "move_func_return" - # "multiple_immutable_borrow" - # "mutable_borrow" - # "mutable_borrow_method_call" - # "mutable_variables" - # "nll_lexical_scope_different" - # "printing" - # "string_from_move_print" - # "string_from_print" - # "struct_lifetime" - # "struct_rect" - # "struct_rect2" - # "struct_string" - # "extra_credit" - "thread_vec" - "thread_vec2" -) - -LOCALEX="src/assets/code_examples" -EX="../rustviz/src/examples" -# Loop through the specified examples -for target in ${targetExamples[@]}; do - printf "building %s...\n" $target - - if ! [[ -d "$EX/$target" ]] - then - mkdir $EX/$target - fi - cp "$LOCALEX/$target/source.rs" "$EX/$target/source.rs" - - # Check if required files are there - if [[ -f "$EX/$target/source.rs" ]] - then - # Check if file headers exist - if ! [[ -f "$EX/$target/main.rs" ]] - then - printf "\ngenerating header for %s..." $target - cd "../rustviz/src/RustvizParse" - cargo run "../examples/$target/source.rs" >/dev/null 2>&1 - cd "../../../rustviz-tutorial" - cp "$EX/$target/main.rs" "$LOCALEX/$target/main.rs" - printf "\nPlease define events and rerun current script\n" - continue - fi - cp "$LOCALEX/$target/main.rs" "$EX/$target/main.rs" - cd "../rustviz/src/" # switch to appropriate folder - # Run svg generation for example - cargo run $target >/dev/null 2>&1 - - # If the svg generation exited with an error or the required SVGs weren't created, report failure and continue - if [[ $? -ne 0 || !(-f "examples/$target/vis_code.svg") || !(-f "examples/$target/vis_timeline.svg") ]]; then - printf "${red}FAILED${end} on SVG generation.\n" - cd ../../rustviz-tutorial - continue - fi - cd ../../rustviz-tutorial - - # Copy files to mdbook directory - mkdir -p "./src/assets/code_examples/$target" - cp "$EX/$target/vis_code.svg" "./src/assets/code_examples/$target/vis_code.svg" - cp "$EX/$target/vis_timeline.svg" "./src/assets/code_examples/$target/vis_timeline.svg" - - else - # Not Necessary (file double check) - printf "${red}FAILED${end}. The required files are not in the examples dir.\n" - fi -done - -# Build mdbook -mdbook build - -# Run HTTP server on docs directory -cd book -python3 -m http.server 8000 \ No newline at end of file diff --git a/copy_assets.sh b/copy_assets.sh deleted file mode 100755 index 67b2479..0000000 --- a/copy_assets.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash - -# This is a convenience script to copy the code examples and corresponding SVG -# visualizations from the rustviz repository. See README.md for more details. -RV='../rustviz' -# printf "copying helper.js and book.js..." -cp "$RV/rustviz_mdbook/mdbook_plugin/helpers.js" "./helpers.js" -cp "$RV/rustviz_mdbook/mdbook_plugin/book.js" "./theme/book.js" -# echo "done" - -printf "copying files into code_examples..." -EX=$RV/src/examples -for DIR in $EX/*/; do - BASENAME=`basename $DIR` - echo $BASENAME - if [[ -f "$DIR/source.rs" && -f "$DIR/vis_code.svg" && -f "$DIR/vis_timeline.svg" ]] - then - mkdir -p "./src/assets/code_examples/$BASENAME" && \ - cp "$DIR/source.rs" "./src/assets/code_examples/$BASENAME/source.rs" && \ - cp "$DIR/vis_code.svg" "./src/assets/code_examples/$BASENAME/vis_code.svg" && \ - cp "$DIR/vis_timeline.svg" "./src/assets/code_examples/$BASENAME/vis_timeline.svg" - else - echo "$BASENAME does not have the required files, skipping." - fi -done -printf "${green}done${END}\n" - -# Copy new files to modified_examples -printf "Copying files into modified_examples..." -for DIR in ./src/assets/modified_examples/*; do - BASENAME=`basename $DIR` - echo $BASENAME - if [[ -f "$EX/$BASENAME/source.rs" && -f "$EX/$BASENAME/vis_code.svg" && -f "$EX/$BASENAME/vis_timeline.svg" ]] - then - cp "$EX/$BASENAME/source.rs" "$DIR/source.rs" && \ - cp "$EX/$BASENAME/vis_code.svg" "$DIR/vis_code.svg" && \ - cp "$EX/$BASENAME/vis_timeline.svg" "$DIR/vis_timeline.svg" - else - echo "$BASENAME does not have the required files, skipping." - fi -done -printf "${green}done${END}\n" - -mdbook build \ No newline at end of file diff --git a/helpers.js b/helpers.js deleted file mode 100644 index 6e756fe..0000000 --- a/helpers.js +++ /dev/null @@ -1,535 +0,0 @@ -const SVG = { - text: { - label: "label", - functionLogo: "label", - }, - path: { - hollow: "timeline_mut", - staticref: "static_ref_line", - mutref: "mut_ref_line", - }, - polyline: "arrow", - circle: "event", - line: "timeline_mut", - use: "function_event", - rect: "structBox", - }; - - /* --------------------- SIMPLE DRIVER --------------------- */ - function helpers(classname) { - // console.log("class name is :", classname) - // create tooltip element before #page-wrapper - let page = document.querySelector("#page-wrapper"); - let tooltip = document.getElementById("svg_tooltip"); - if (!tooltip) { - tooltip = document.createElement("p"); - tooltip.id = "svg_tooltip"; - tooltip.style.cssText = - "position: absolute; padding: 0.5em; font-size: 0.75em; border-radius: 8px;" + - "font-family: 'Trebuchet MS', Helvetica, sans-serif;" + - "background: rgb(70, 70, 70, 0.6); color: white; z-index: 100; display: none;"; - page.parentNode.insertBefore(tooltip, page); - } - - displayFn(classname); - displayTooltip(tooltip, classname); - } - - /* --------------------- FUNCTION HIGHLIGHT --------------------- */ - - // change function name color on hover - function displayFn(classname) { - // get svg elements - let vis_num = document.getElementsByClassName(classname); - let code_obj = vis_num[0]; - let tl_obj = vis_num[1]; - let c_svg = code_obj.contentDocument.firstChild; - let tl_svg = tl_obj.contentDocument.firstChild; - // get elements that will trigger function - let triggers = tl_svg.getElementsByClassName("fn-trigger"); - var functions = c_svg.getElementsByClassName("fn"); - - for (let i = 0; i < triggers.length; i++) { - triggers[i].addEventListener("mouseover", showFn); - triggers[i].addEventListener("mouseout", hideFn); - } - - function showFn(evt) { - // console.log("showFn") - // get target attributes - let evt_hash = evt.target.dataset.hash; - - for (let i = 0; i < functions.length; i++) { - // if hashes match, temporarily change color - if (functions[i].getAttribute("hash") == evt_hash) { - functions[i].dataset.hash = evt_hash; - } - } - } - - function hideFn() { - // console.log("hideFn"); - // // send hover msg to API - // var xhr = new XMLHttpRequest(); - // xhr.open("POST", "/action/hover", true); // could add authentification info - // xhr.setRequestHeader("Content-Type", "application/json"); - // xhr.send( - // JSON.stringify({ - // action_type: "hideFn", - // }) - // ); - // reset to hash 0, styling to black on mouseout - for (let i = 0; i < functions.length; i++) { - functions[i].dataset.hash = 0; - } - } - } - - /* --------------------- SVG CODE-RELATED FUNCTIONS --------------------- */ - - // resize code block to fit comments - function sizeToFit(object) { - // Case for Chrome loading - if (navigator.userAgent.indexOf("Chrome") !== -1) { - object.addEventListener( - "load", - function () { - let svg_doc = object.contentDocument; - let code_width = svg_doc.getElementById("code").getBBox().width; - let new_width = Math.max(code_width + 30, 400); - svg_doc.firstChild.setAttribute("width", new_width + "px"); - }, - { once: true } - ); - } else { - if (object.contentDocument.readyState === "complete") { - let svg_doc = object.contentDocument; - let code_width = svg_doc.getElementById("code").getBBox().width; - let new_width = Math.max(code_width + 30, 400); - svg_doc.firstChild.setAttribute("width", new_width + "px"); - } - } - } - - /* --------------------- TOOLTIP-RELATED FUNCTIONS --------------------- */ - - // change tooltip text on hover - function displayTooltip(tooltip, classname) { - // get svg elements - let tl_obj = document.getElementsByClassName(classname)[1]; - let tl_svg = tl_obj.contentDocument.firstChild; - // get elements that will trigger function - let triggers = tl_svg.getElementsByClassName("tooltip-trigger"); - - let start_line = 0; - let end_line = 0; - - // track time - var time_start = null; - - for (let i = 0; i < triggers.length; i++) { - // prevent adding duplicate listeners - if (triggers[i].classList.contains("listener")) break; - else triggers[i].classList.add("listener"); - - triggers[i].addEventListener("mousemove", showTooltip); - triggers[i].addEventListener("mouseleave", hideTooltip); - triggers[i].addEventListener("mouseenter", insertUnderline); - } - - function showTooltip(e) { - // console.log("showTooltip") - // only set time once, prevent from changing every time mouse moves - if (!time_start) time_start = Date.now(); - - let mouse = mousePos(e, tl_obj); - tooltip.style.transform = "translate(" + mouse.x + "px, " + mouse.y + "px)"; - tooltip.style.display = "block"; - - let text = e.currentTarget.getAttributeNS(null, "data-tooltip-text"); - tooltip.innerHTML = text; - - // if out of bounds, break text into two lines - if (tooltip.getBoundingClientRect().right >= document.body.clientWidth) - breakText(text, tooltip); - } - - // function parseText(text) { - // let split_text = text.split(" "); - // let words = []; - - // for (const elt of split_text) { - // if (elt.startsWith("<")) { - // parse1 = elt.substr(elt.indexOf(">") + 1); - // parse2 = parse1.substr(0, parse1.indexOf("<")); - // words.push(parse2); - // } - // else { - // words.push(elt); - // } - // } - - // result = ""; - // for (let elt of words) { - // result += elt; - // result += " "; - // } - - // return result; - // } - - function parseText(text) { - result = text; - while (result.indexOf("<") !== -1) { - left = result.substr(0, result.indexOf("<")); - right = result.substr(result.indexOf(">") + 1); - result = left + " " + right; - } - - return result; - } - - function hideTooltip(e) { - // console.log("hideTooltip"); - // console.log(e); - // console.log(e.data-tooltip-text); - // console.log(e.currentTarget.ownerSVGElement.id); - - let tgt = e.currentTarget; - // console.log(tgt.data-tooltip-text); - - let text = e.currentTarget.getAttributeNS(null, "data-tooltip-text"); - text = parseText(text); - - let e_label = - tgt.tagName === "text" - ? SVG["text"][tgt.classList[0]] - : tgt.tagName === "path" - ? SVG["path"][tgt.classList[0]] - : SVG[tgt.tagName]; - - // console.log(e_label); - - let now = Date.now(); - - let hover_time = now - time_start; // time in ms - - var xhr = new XMLHttpRequest(); - xhr.open("POST", "/action/hover", true); // could add authentification info - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.send( - JSON.stringify({ - svg_name: e.currentTarget.ownerSVGElement.id, - hover_item: e_label, - hover_time: hover_time, - start: time_start, - end: now, - hover_message: text, - start_line: parseInt(start_line), - end_line: parseInt(end_line), - }) - ); - // console.log(xhr); - - tooltip.style.display = "none"; - tooltip.innerHTML = ""; - - start_line = 0; - end_line = 0; - - // only track hovering after mouse leaves element - gtag("event", "tooltip_hover", { - event_label: e_label, - }); - - gtag("event", e_label, { - hover_time: Date.now() - time_start, // time in ms - }); - time_start = null; // reset - - removeUnderline(e, classname); - } - - /* ---- SHOW RELEVANT LINES ---- */ - function insertUnderline(e) { - // console.log("insertUnderline"); - let doc = document.getElementsByClassName(classname + " code_panel")[0] - .contentDocument; //code_panel - let begin = 0, - end = 0; - if (e.currentTarget.tagName === "path") { - let arr = e.currentTarget.getAttribute("d").split(" "); - if (e.currentTarget.parentNode.id === "ref_line") { - begin = parseInt(arr[2]); - end = parseInt(begin) + 2 * parseInt(arr[5]) + parseInt(arr[7]) + 5; // + 5 to include last line - } else { - begin = parseInt(arr[7]); - end = parseInt(arr[3]); - } - } else if (e.currentTarget.tagName === "line") { - begin = e.currentTarget.getAttribute("y1"); - end = e.currentTarget.getAttribute("y2"); - } else { - let pos; - if (e.currentTarget.tagName === "circle") { - begin = end = parseInt(e.currentTarget.getAttribute("cy")) + 5; - } else if (e.currentTarget.tagName === "use") { - begin = end = parseInt(e.currentTarget.getAttribute("y")) + 5; - } else if (e.currentTarget.tagName === "polyline") { - let arr = e.currentTarget.getAttribute("points").split(" "); - begin = end = parseInt(arr[1]) + 5; - } else { - // e.currentTarget.tagName === 'text' - begin = end = parseInt(e.currentTarget.getAttribute("y")); - } - } - - // add underlining to every relevant line - let lines = doc.getElementById("code").children; - let len = lines.length; // prevent len from changing - - // start_line and end_line are computed from variables begin and end. - - // begin and end are y-cordinates of the html elements on the website. - // They might have a remainder of 25 or 0 when divided by 30 - // depending on the graphs generated by the code. - // Hence, we handle these situations differently - if (parseInt(begin) % 30 !== 0) { - start_line = (parseInt(begin) + 5 - 60) / 30; - } - else { - start_line = (parseInt(begin) - 60) / 30; - } - - if (parseInt(end) % 30 !== 0) { - end_line = (parseInt(end) + 5 - 60) / 30; - } - else { - end_line = (parseInt(end) - 60) / 30; - } - - - for (let i = 0; i < len; ++i) { - let ly = parseInt(lines[i].getAttribute("y")); - if (ly >= begin && ly <= end) { - - // only underline relevant code - let emph = doc.createElementNS("http://www.w3.org/2000/svg", "text"); - emph.setAttribute("class", "code emph"); - emph.setAttribute("x", "25"); - emph.setAttribute("y", ly + 3); // +3 to hang just under text - emph.innerHTML = new Array( - Math.floor(lines[i].getBBox().width / 8) // size of '_' = 8 - ).join("_"); // string with all underscores - doc.getElementById("code").appendChild(emph); - } - } - } - } - - // track mouse movement - function mousePos(evt, obj) { - let x_pos = evt.clientX + obj.getBoundingClientRect().x + 15; // offset from svg start + svg offset - let y_pos = evt.clientY + obj.getBoundingClientRect().y + window.scrollY + 45; // baseline hanging - - return { - //object - x: Math.round(x_pos), - y: Math.round(y_pos), - }; - } - - function removeUnderline(e, classname) { - let doc = document.getElementsByClassName(classname + " code_panel")[0] - .contentDocument; //code_panel - let arr = doc.getElementsByClassName("emph"); - for (let i = arr.length - 1; i >= 0; --i) { - arr[i].remove(); - } - } - - // adjust text box - function breakText(text, tooltip) { - // combine span into one element - let split_text = text.split(" "); - let words = []; - let last = 0, - span = false; - for (const elt of split_text) { - if (elt.startsWith("<")) { - span = true; - words.push(elt); - last = words.length - 1; - } else if (elt.startsWith("!important")) { - span = false; - words[last] += elt; - } else { - if (span) { - words[last] = words[last] + " " + elt; - } else { - words.push(elt); - } - } - } - - // adjust size and split text based on page boundary - tooltip.innerHTML = ""; - // console.log('enter?'); - let left = tooltip.getBoundingClientRect().left; - for (const word of words) { - // console.log('yes?'); - // console.log(word); - tooltip.innerHTML += word + " "; - if (left + tooltip.clientWidth > document.body.clientWidth - 20) { - // reset tooltip text and break into new lines - let idx = tooltip.innerHTML.lastIndexOf( - " ", - tooltip.innerHTML.length - 2 - ); - let temp = tooltip.innerHTML.substr(0, idx); - let other = tooltip.innerHTML.substr(idx + 1); - - tooltip.innerHTML = ""; - tooltip.innerHTML += temp; - tooltip.innerHTML += "
" + other; - } - } - } - - /* --------------- TOGGLE ALL SVGS --------------- */ - function toggleAll(turn_on) { - let evt = new MouseEvent("click", { - bubbles: true, - cancelable: true, - view: window, - }); - - let arr = document.getElementsByClassName("toggle-button"); - for (const obj of arr) { - if (turn_on && obj.classList.contains("fa-toggle-off")) { - obj.dispatchEvent(evt); - } else if (!turn_on && obj.classList.contains("fa-toggle-on")) { - obj.dispatchEvent(evt); - } - } - } - - /* --------------- TOGGLE ALL STRUCTS --------------- */ - function toggleStruct(turn_on) { - var evt = new MouseEvent("click", { - bubbles: true, - cancelable: true, - view: window, - }); - - var arr = document.getElementsByClassName("non-struct"); - for (const obj of arr) { - if (turn_on && obj.classList.contains("fa-toggle-off")) { - obj.dispatchEvent(evt); - } else if (!turn_on && obj.classList.contains("fa-toggle-on")) { - obj.dispatchEvent(evt); - } - } - } - - /* --------------- DETECT TAB CLOSE --------------- */ - // window.addEventListener("beforeunload", (event) => { - // chapter_list = document.getElementsByClassName("expanded"); - // start = new Date(); - // for (chapter of chapter_list) { - // // console.log(chapter.textContent); - // chapter.firstChild.addEventListener("click", function(e) { - // e.preventDefault(); - // OnOneClick(window.location.pathname, start); - // window.location = this.href; - // // if(e.target && e.target.nodeName == "A") { - // // OnOneClick(window.location.pathname, start); - // // } - // }, false); - // } - - // var xhr = new XMLHttpRequest(); - // xhr.open("POST", "/action/close", true); // could add authentification info - // xhr.setRequestHeader("Content-Type", "application/json"); - // xhr.send( - // JSON.stringify({ - // action_type: "closed", - // }) - // ); - // delete event["returnValue"]; - // }); - - /* --------------- DETECT PAGE LOAD --------------- */ - window.addEventListener("load", function () { - chapter_list = document.getElementsByClassName("expanded"); - start = new Date(); - // the following handler will send a POST request when a tab is closed or switched - window.addEventListener("beforeunload", (event) => { - OnOneClick(window.location.pathname, start); - }) - }); - - function callback(e) { - var e = window.e || e; - - alert('The link is: ' + e.target.href); - } - - function resume(s) { - return true; - } - - var OnOneClick = function (chapter, start) { - // Your click handler - time_onpage = new Date() - start; - end_time = new Date(); - var xhr = new XMLHttpRequest(); - xhr.open("POST", "/action/switch", true); // could add authentification info - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.send( - JSON.stringify({ - directory: chapter, - time_elpse: time_onpage, - start_time: start.getTime(), - end_time: end_time.getTime(), - }) - ); - console.log(xhr) - }; - - /*window.onload = function () { - var correct_doc = (document.getElementsByClassName('active')[0].attributes.href.value === 'ch04-01-what-is-ownership.html' - || document.getElementsByClassName('active')[0].attributes.href.value === 'ch04-02-references-and-borrowing.html'); - - if (correct_doc) { - let top_btns = document.getElementsByClassName('left-buttons'); - - var eye = document.getElementById('viz-toggle'); - var struct_eye = document.getElementById('viz-struct-toggle') - - if (!eye) { - eye = document.createElement('button'); - eye.id = 'viz-toggle'; - eye.className = 'icon-button fa fa-eye'; - eye.title = 'Toggle all visualizations'; - top_btns[0].insertBefore(eye, top_btns[0].lastElementChild); - } - - eye.addEventListener('click', function (e) { - if (e.currentTarget.classList.contains('fa-eye')) { - // on button click, show all visualizations - e.currentTarget.classList.remove('fa-eye'); - e.currentTarget.classList.add('fa-eye-slash'); - - toggleAll(true); - } else if (e.currentTarget.classList.contains('fa-eye-slash')) { - // on button click, hide all visualizations - e.currentTarget.classList.remove('fa-eye-slash'); - e.currentTarget.classList.add('fa-eye'); - - toggleAll(false); - } - }); - } - };*/ - diff --git a/main_screenshot.png b/main_screenshot.png deleted file mode 100644 index dc09b86..0000000 Binary files a/main_screenshot.png and /dev/null differ diff --git a/src/additional_resources.md b/src/additional_resources.md index cdbd361..e853844 100644 --- a/src/additional_resources.md +++ b/src/additional_resources.md @@ -1,10 +1 @@ # Additional Resources - -This tutorial introduced you to the basics of Rust and its ownership and -borrowing system. If you are interested in diving deeper into Rust, here are -some helpful resources: - -- [The Rust Programming Language (a.k.a. "the book")](https://doc.rust-lang.org/book/): An overview of Rust from first principles -- [Rustlings](https://github.com/rust-lang/rustlings/): Small exercises in Rust -- [Rust By Example](https://doc.rust-lang.org/stable/rust-by-example/): Introduces Rust concepts by through examples, as the name suggests -- [Learn Rust](https://www.rust-lang.org/learn): Contains links to many different Rust learning resources including the ones above \ No newline at end of file diff --git a/src/assets/code_examples/copy/source.rs b/src/assets/code_examples/copy/source.rs deleted file mode 100644 index 49c51b2..0000000 --- a/src/assets/code_examples/copy/source.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - let x = 5; - let y = x; -} \ No newline at end of file diff --git a/src/assets/code_examples/copy/vis_code.svg b/src/assets/code_examples/copy/vis_code.svg deleted file mode 100644 index 8bc3f06..0000000 --- a/src/assets/code_examples/copy/vis_code.svg +++ /dev/null @@ -1,178 +0,0 @@ - - - examples/copy/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = 5; - 3 let y = x; - 4 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/copy/vis_timeline.svg b/src/assets/code_examples/copy/vis_timeline.svg deleted file mode 100644 index f2b61a9..0000000 --- a/src/assets/code_examples/copy/vis_timeline.svg +++ /dev/null @@ -1,261 +0,0 @@ - - - examples/copy/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/extra_credit/source.rs b/src/assets/code_examples/extra_credit/source.rs deleted file mode 100644 index a7338b1..0000000 --- a/src/assets/code_examples/extra_credit/source.rs +++ /dev/null @@ -1,18 +0,0 @@ -fn f(s1: &String) { - s1.push_str(" 490!"); -} - -fn main() { - let mut num = 490; - let mut x = String::from("EECS"); - { - let y = &mut x; - f(y); - let mut s2 = x; - s2.push_str(" Woo!"); - println!("{}", s2); - - let n1 = num; - println!("{}", n1); - } -} \ No newline at end of file diff --git a/src/assets/code_examples/extra_credit/vis_code.svg b/src/assets/code_examples/extra_credit/vis_code.svg deleted file mode 100644 index 75699cb..0000000 --- a/src/assets/code_examples/extra_credit/vis_code.svg +++ /dev/null @@ -1,192 +0,0 @@ - - - examples/extra_credit/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn f(s1: &String) { - 2 s1.push_str(" 490!"); - 3 } - 4 - 5 fn main() { - 6 let mut num = 490; - 7 let mut x = String::from("EECS"); - 8 { - 9 let y = &mut x; - 10 f(y); - 11 let mut s2 = x; - 12 s2.push_str(" Woo!"); - 13 println!("{}", s2); - 14 - 15 let n1 = num; - 16 println!("{}", n1); - 17 } - 18 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/extra_credit/vis_timeline.svg b/src/assets/code_examples/extra_credit/vis_timeline.svg deleted file mode 100644 index 902e933..0000000 --- a/src/assets/code_examples/extra_credit/vis_timeline.svg +++ /dev/null @@ -1,236 +0,0 @@ - - - examples/extra_credit/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/func_take_ownership/source.rs b/src/assets/code_examples/func_take_ownership/source.rs deleted file mode 100644 index cdea2a0..0000000 --- a/src/assets/code_examples/func_take_ownership/source.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - let s = String::from("hello"); - takes_ownership(s); - // println!("{}", s) // won't compile if added -} - -fn takes_ownership(some_string: String) { - println!("{}", some_string); -} \ No newline at end of file diff --git a/src/assets/code_examples/func_take_ownership/vis_code.svg b/src/assets/code_examples/func_take_ownership/vis_code.svg deleted file mode 100644 index f10aa46..0000000 --- a/src/assets/code_examples/func_take_ownership/vis_code.svg +++ /dev/null @@ -1,183 +0,0 @@ - - - examples/func_take_ownership/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let s = String::from("hello"); - 3 takes_ownership(s); - 4 // println!("{}", s) // won't compile if added - 5 } - 6 - 7 fn takes_ownership(some_string: String) { - 8 println!("{}", some_string); - 9 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/func_take_ownership/vis_timeline.svg b/src/assets/code_examples/func_take_ownership/vis_timeline.svg deleted file mode 100644 index 23490a1..0000000 --- a/src/assets/code_examples/func_take_ownership/vis_timeline.svg +++ /dev/null @@ -1,276 +0,0 @@ - - - examples/func_take_ownership/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - some_string - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/func_take_return_ownership/source.rs b/src/assets/code_examples/func_take_return_ownership/source.rs deleted file mode 100644 index 46846cc..0000000 --- a/src/assets/code_examples/func_take_return_ownership/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn take_and_return_ownership(some_string : String) -> String { - println!("{}", some_string); - some_string -} - -fn main() { - let mut s = String::from("hello"); - s = take_and_return_ownership(s); - println!("{}", s); // OK -} \ No newline at end of file diff --git a/src/assets/code_examples/func_take_return_ownership/vis_code.svg b/src/assets/code_examples/func_take_return_ownership/vis_code.svg deleted file mode 100644 index 8da896f..0000000 --- a/src/assets/code_examples/func_take_return_ownership/vis_code.svg +++ /dev/null @@ -1,184 +0,0 @@ - - - examples/func_take_return_ownership/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn take_and_return_ownership(some_string : String) -> String { - 2 println!("{}", some_string); - 3 some_string - 4 } - 5 - 6 fn main() { - 7 let mut s = String::from("hello"); - 8 s = take_and_return_ownership(s); - 9 println!("{}", s); // OK - 10 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/func_take_return_ownership/vis_timeline.svg b/src/assets/code_examples/func_take_return_ownership/vis_timeline.svg deleted file mode 100644 index ce6acb3..0000000 --- a/src/assets/code_examples/func_take_return_ownership/vis_timeline.svg +++ /dev/null @@ -1,300 +0,0 @@ - - - examples/func_take_return_ownership/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - some_string - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/function/source.rs b/src/assets/code_examples/function/source.rs deleted file mode 100644 index e0abbc1..0000000 --- a/src/assets/code_examples/function/source.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - let six = plus_one(5); -} - -fn plus_one(x: i32) -> i32 { - x + 1 -} \ No newline at end of file diff --git a/src/assets/code_examples/function/vis_code.svg b/src/assets/code_examples/function/vis_code.svg deleted file mode 100644 index dd11a7e..0000000 --- a/src/assets/code_examples/function/vis_code.svg +++ /dev/null @@ -1,181 +0,0 @@ - - - examples/function/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let six = plus_one(5); - 3 } - 4 - 5 fn plus_one(x: i32) -> i32 { - 6 x + 1 - 7 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/function/vis_timeline.svg b/src/assets/code_examples/function/vis_timeline.svg deleted file mode 100644 index 4c9e2fb..0000000 --- a/src/assets/code_examples/function/vis_timeline.svg +++ /dev/null @@ -1,262 +0,0 @@ - - - examples/function/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - six - x - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/hatra1/source.rs b/src/assets/code_examples/hatra1/source.rs deleted file mode 100644 index 1b546bc..0000000 --- a/src/assets/code_examples/hatra1/source.rs +++ /dev/null @@ -1,11 +0,0 @@ -fn main() { - let s = String::from("hello"); - takes_ownership(s); - let mut x = 5; - let y = x; - x = 6; -} - -fn takes_ownership(some_string: String) { - println!("{}", some_string); -} \ No newline at end of file diff --git a/src/assets/code_examples/hatra1/vis_code.svg b/src/assets/code_examples/hatra1/vis_code.svg deleted file mode 100644 index 265aacf..0000000 --- a/src/assets/code_examples/hatra1/vis_code.svg +++ /dev/null @@ -1,185 +0,0 @@ - - - examples/hatra1/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let s = String::from("hello"); - 3 takes_ownership(s); - 4 let mut x = 5; - 5 let y = x; - 6 x = 6; - 7 } - 8 - 9 fn takes_ownership(some_string: String) { - 10 println!("{}", some_string); - 11 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/hatra1/vis_timeline.svg b/src/assets/code_examples/hatra1/vis_timeline.svg deleted file mode 100644 index 6d5c47b..0000000 --- a/src/assets/code_examples/hatra1/vis_timeline.svg +++ /dev/null @@ -1,307 +0,0 @@ - - - examples/hatra1/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - x - y - some_string - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/hatra1_test/source.rs b/src/assets/code_examples/hatra1_test/source.rs deleted file mode 100644 index 1b546bc..0000000 --- a/src/assets/code_examples/hatra1_test/source.rs +++ /dev/null @@ -1,11 +0,0 @@ -fn main() { - let s = String::from("hello"); - takes_ownership(s); - let mut x = 5; - let y = x; - x = 6; -} - -fn takes_ownership(some_string: String) { - println!("{}", some_string); -} \ No newline at end of file diff --git a/src/assets/code_examples/hatra1_test/vis_code.svg b/src/assets/code_examples/hatra1_test/vis_code.svg deleted file mode 100644 index 55650c4..0000000 --- a/src/assets/code_examples/hatra1_test/vis_code.svg +++ /dev/null @@ -1,185 +0,0 @@ - - - examples/hatra1_test/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let s = String::from("hello"); - 3 takes_ownership(s); - 4 let mut x = 5; - 5 let y = x; - 6 x = 6; - 7 } - 8 - 9 fn takes_ownership(some_string: String) { - 10 println!("{}", some_string); - 11 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/hatra1_test/vis_timeline.svg b/src/assets/code_examples/hatra1_test/vis_timeline.svg deleted file mode 100644 index 599521a..0000000 --- a/src/assets/code_examples/hatra1_test/vis_timeline.svg +++ /dev/null @@ -1,292 +0,0 @@ - - - examples/hatra1_test/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - x - y - some_string - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/hatra2/source.rs b/src/assets/code_examples/hatra2/source.rs deleted file mode 100644 index 61992d0..0000000 --- a/src/assets/code_examples/hatra2/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn main(){ - let mut s = String::from("hello"); - - let r1 = &s; - let r2 = &s; - assert!(compare_strings(r1, r2)); - - let r3 = &mut s; - clear_string(r3); -} \ No newline at end of file diff --git a/src/assets/code_examples/hatra2/vis_code.svg b/src/assets/code_examples/hatra2/vis_code.svg deleted file mode 100644 index 8021402..0000000 --- a/src/assets/code_examples/hatra2/vis_code.svg +++ /dev/null @@ -1,185 +0,0 @@ - - - examples/hatra2/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main(){ - 2 let mut s = String::from("hello"); - 3 - 4 let r1 = &s; - 5 let r2 = &s; - 6 assert!(compare_strings(r1, r2)); - 7 - 8 - 9 let r3 = &mut s; - 10 clear_string(r3); - 11 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/hatra2/vis_timeline.svg b/src/assets/code_examples/hatra2/vis_timeline.svg deleted file mode 100644 index 0a6ed68..0000000 --- a/src/assets/code_examples/hatra2/vis_timeline.svg +++ /dev/null @@ -1,365 +0,0 @@ - - - examples/hatra2/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - r1|*r1 - r2|*r2 - r3|*r3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/immutable_borrow/source.rs b/src/assets/code_examples/immutable_borrow/source.rs deleted file mode 100644 index b05ff3c..0000000 --- a/src/assets/code_examples/immutable_borrow/source.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - let x = String::from("hello"); - f(&x); - println!("{}", x); -} - -fn f(s : &String) { - println!("{}", *s); -} \ No newline at end of file diff --git a/src/assets/code_examples/immutable_borrow/vis_code.svg b/src/assets/code_examples/immutable_borrow/vis_code.svg deleted file mode 100644 index dc90f9e..0000000 --- a/src/assets/code_examples/immutable_borrow/vis_code.svg +++ /dev/null @@ -1,183 +0,0 @@ - - - examples/immutable_borrow/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 f(&x); - 4 println!("{}", x); - 5 } - 6 - 7 fn f(s : &String) { - 8 println!("{}", *s); - 9 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/immutable_borrow/vis_timeline.svg b/src/assets/code_examples/immutable_borrow/vis_timeline.svg deleted file mode 100644 index cc96572..0000000 --- a/src/assets/code_examples/immutable_borrow/vis_timeline.svg +++ /dev/null @@ -1,298 +0,0 @@ - - - examples/immutable_borrow/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - s|*s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/immutable_borrow_method_call/source.rs b/src/assets/code_examples/immutable_borrow_method_call/source.rs deleted file mode 100644 index fb1ade4..0000000 --- a/src/assets/code_examples/immutable_borrow_method_call/source.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - let s = String::from("hello"); - let len1 = String::len(&s); - let len2 = s.len(); // shorthand for the above - println!("len1 = {} = len2 = {}", len1, len2); -} \ No newline at end of file diff --git a/src/assets/code_examples/immutable_borrow_method_call/vis_code.svg b/src/assets/code_examples/immutable_borrow_method_call/vis_code.svg deleted file mode 100644 index d29b266..0000000 --- a/src/assets/code_examples/immutable_borrow_method_call/vis_code.svg +++ /dev/null @@ -1,180 +0,0 @@ - - - examples/immutable_borrow_method_call/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let s = String::from("hello"); - 3 let len1 = String::len(&s); - 4 let len2 = s.len(); // shorthand for the above - 5 println!("len1 = {} = len2 = {}", len1, len2); - 6 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/immutable_borrow_method_call/vis_timeline.svg b/src/assets/code_examples/immutable_borrow_method_call/vis_timeline.svg deleted file mode 100644 index 5c6fbbd..0000000 --- a/src/assets/code_examples/immutable_borrow_method_call/vis_timeline.svg +++ /dev/null @@ -1,324 +0,0 @@ - - - examples/immutable_borrow_method_call/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - len1 - len2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/immutable_variable/source.rs b/src/assets/code_examples/immutable_variable/source.rs deleted file mode 100644 index 1a4d6f6..0000000 --- a/src/assets/code_examples/immutable_variable/source.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - let x = 5; -} \ No newline at end of file diff --git a/src/assets/code_examples/immutable_variable/vis_code.svg b/src/assets/code_examples/immutable_variable/vis_code.svg deleted file mode 100644 index 9453e41..0000000 --- a/src/assets/code_examples/immutable_variable/vis_code.svg +++ /dev/null @@ -1,177 +0,0 @@ - - - examples/immutable_variable/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = 5; - 3 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/immutable_variable/vis_timeline.svg b/src/assets/code_examples/immutable_variable/vis_timeline.svg deleted file mode 100644 index bf21958..0000000 --- a/src/assets/code_examples/immutable_variable/vis_timeline.svg +++ /dev/null @@ -1,246 +0,0 @@ - - - examples/immutable_variable/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/move_assignment/source.rs b/src/assets/code_examples/move_assignment/source.rs deleted file mode 100644 index b5b8d61..0000000 --- a/src/assets/code_examples/move_assignment/source.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let x = String::from("hello"); - let mut y = String::from("test"); - y = x; -} \ No newline at end of file diff --git a/src/assets/code_examples/move_assignment/vis_code.svg b/src/assets/code_examples/move_assignment/vis_code.svg deleted file mode 100644 index 964718f..0000000 --- a/src/assets/code_examples/move_assignment/vis_code.svg +++ /dev/null @@ -1,179 +0,0 @@ - - - examples/move_assignment/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 let mut y = String::from("test"); - 4 y = x; - 5 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/move_assignment/vis_timeline.svg b/src/assets/code_examples/move_assignment/vis_timeline.svg deleted file mode 100644 index da51429..0000000 --- a/src/assets/code_examples/move_assignment/vis_timeline.svg +++ /dev/null @@ -1,270 +0,0 @@ - - - examples/move_assignment/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/move_different_scope/source.rs b/src/assets/code_examples/move_different_scope/source.rs deleted file mode 100644 index 9c0d728..0000000 --- a/src/assets/code_examples/move_different_scope/source.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - let x = String::from("hello"); - let z = { - let y = x; - println("{}", y); - // ... - }; - println!("Hello, world!"); -} \ No newline at end of file diff --git a/src/assets/code_examples/move_different_scope/vis_code.svg b/src/assets/code_examples/move_different_scope/vis_code.svg deleted file mode 100644 index 2d01c46..0000000 --- a/src/assets/code_examples/move_different_scope/vis_code.svg +++ /dev/null @@ -1,183 +0,0 @@ - - - examples/move_different_scope/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 let z = { - 4 let y = x; - 5 println!("{}", y); - 6 // ... - 7 }; - 8 println!("Hello, world!"); - 9 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/move_different_scope/vis_timeline.svg b/src/assets/code_examples/move_different_scope/vis_timeline.svg deleted file mode 100644 index acc3af2..0000000 --- a/src/assets/code_examples/move_different_scope/vis_timeline.svg +++ /dev/null @@ -1,285 +0,0 @@ - - - examples/move_different_scope/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y - z - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/move_func_return/source.rs b/src/assets/code_examples/move_func_return/source.rs deleted file mode 100644 index 6798b2b..0000000 --- a/src/assets/code_examples/move_func_return/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn f() { - let x = String::from("hello"); - // ... - x -} - -fn main() { - let s = f(); - println!("{}", s); -} \ No newline at end of file diff --git a/src/assets/code_examples/move_func_return/vis_code.svg b/src/assets/code_examples/move_func_return/vis_code.svg deleted file mode 100644 index afe7c7d..0000000 --- a/src/assets/code_examples/move_func_return/vis_code.svg +++ /dev/null @@ -1,184 +0,0 @@ - - - examples/move_func_return/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn f() { - 2 let x = String::from("hello"); - 3 // ... - 4 x - 5 } - 6 - 7 fn main() { - 8 let s = f(); - 9 println!("{}", s); - 10 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/move_func_return/vis_timeline.svg b/src/assets/code_examples/move_func_return/vis_timeline.svg deleted file mode 100644 index 0ce8adb..0000000 --- a/src/assets/code_examples/move_func_return/vis_timeline.svg +++ /dev/null @@ -1,276 +0,0 @@ - - - examples/move_func_return/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/multiple_immutable_borrow/source.rs b/src/assets/code_examples/multiple_immutable_borrow/source.rs deleted file mode 100644 index 87f645a..0000000 --- a/src/assets/code_examples/multiple_immutable_borrow/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn main() { - let x = String::from("hello"); - let y = &x; - let z = &x; - f(y, z); -} - -fn f(s1 : &String, s2 : &String) { - println!("{} and {}", s1, s2); -} \ No newline at end of file diff --git a/src/assets/code_examples/multiple_immutable_borrow/vis_code.svg b/src/assets/code_examples/multiple_immutable_borrow/vis_code.svg deleted file mode 100644 index 1996a2e..0000000 --- a/src/assets/code_examples/multiple_immutable_borrow/vis_code.svg +++ /dev/null @@ -1,185 +0,0 @@ - - - examples/multiple_immutable_borrow/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 let y = &x; - 4 let z = &x; - 5 f(y, z); - 6 - 7 } - 8 - 9 fn f(s1 : &String, s2 : &String) { - 10 println!("{} and {}", s1, s2); - 11 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/multiple_immutable_borrow/vis_timeline.svg b/src/assets/code_examples/multiple_immutable_borrow/vis_timeline.svg deleted file mode 100644 index 9f4639c..0000000 --- a/src/assets/code_examples/multiple_immutable_borrow/vis_timeline.svg +++ /dev/null @@ -1,378 +0,0 @@ - - - examples/multiple_immutable_borrow/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y|*y - z|*z - s1|*s1 - s2|*s2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/mutable_borrow/source.rs b/src/assets/code_examples/mutable_borrow/source.rs deleted file mode 100644 index f49b480..0000000 --- a/src/assets/code_examples/mutable_borrow/source.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - let mut x = String::from("Hello"); - world(&mut x); - println!("{}", x); -} - -fn world(s : &mut String) { - s.push_str(", world"); -} \ No newline at end of file diff --git a/src/assets/code_examples/mutable_borrow/vis_code.svg b/src/assets/code_examples/mutable_borrow/vis_code.svg deleted file mode 100644 index 631f094..0000000 --- a/src/assets/code_examples/mutable_borrow/vis_code.svg +++ /dev/null @@ -1,183 +0,0 @@ - - - examples/mutable_borrow/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let mut x = String::from("hello"); - 3 world(&mut &x); - 4 println!("{}", x); - 5 } - 6 - 7 fn world(s : &mut String) { - 8 s.push_str(", world"); - 9 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/mutable_borrow/vis_timeline.svg b/src/assets/code_examples/mutable_borrow/vis_timeline.svg deleted file mode 100644 index 0e2476f..0000000 --- a/src/assets/code_examples/mutable_borrow/vis_timeline.svg +++ /dev/null @@ -1,292 +0,0 @@ - - - examples/mutable_borrow/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - s|*s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/mutable_borrow_method_call/source.rs b/src/assets/code_examples/mutable_borrow_method_call/source.rs deleted file mode 100644 index 4a8cfd2..0000000 --- a/src/assets/code_examples/mutable_borrow_method_call/source.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - let mut s1 = String::from("Hello"); - let s2 = String::from(", world"); - String::push_str(&mut s1, &s2); - s1.push_str(&s2); // shorthand for the above - println!("{}", s1); // prints "Hello, world, world" -} \ No newline at end of file diff --git a/src/assets/code_examples/mutable_borrow_method_call/vis_code.svg b/src/assets/code_examples/mutable_borrow_method_call/vis_code.svg deleted file mode 100644 index 992ad01..0000000 --- a/src/assets/code_examples/mutable_borrow_method_call/vis_code.svg +++ /dev/null @@ -1,181 +0,0 @@ - - - examples/mutable_borrow_method_call/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let mut s1 = String::from("hello"); - 3 let s2 = String::from(", world"); - 4 String::push_str(&mut s1, &s2); - 5 s1.push_str(&s2); // shorthand for the above - 6 println!("{}", s1); // prints "Hello, world, world" - 7 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/mutable_borrow_method_call/vis_timeline.svg b/src/assets/code_examples/mutable_borrow_method_call/vis_timeline.svg deleted file mode 100644 index 7b7531b..0000000 --- a/src/assets/code_examples/mutable_borrow_method_call/vis_timeline.svg +++ /dev/null @@ -1,319 +0,0 @@ - - - examples/mutable_borrow_method_call/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s1 - s2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/mutable_variables/source.rs b/src/assets/code_examples/mutable_variables/source.rs deleted file mode 100644 index e53756b..0000000 --- a/src/assets/code_examples/mutable_variables/source.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - let mut x = 5; - x = 6; //OK -} \ No newline at end of file diff --git a/src/assets/code_examples/mutable_variables/vis_code.svg b/src/assets/code_examples/mutable_variables/vis_code.svg deleted file mode 100644 index 73ec967..0000000 --- a/src/assets/code_examples/mutable_variables/vis_code.svg +++ /dev/null @@ -1,178 +0,0 @@ - - - examples/mutable_variables/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let mut x = 5; - 3 x = 6; //OK - 4 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/mutable_variables/vis_timeline.svg b/src/assets/code_examples/mutable_variables/vis_timeline.svg deleted file mode 100644 index 0d84d78..0000000 --- a/src/assets/code_examples/mutable_variables/vis_timeline.svg +++ /dev/null @@ -1,252 +0,0 @@ - - - examples/mutable_variables/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/nll_lexical_scope_different/source.rs b/src/assets/code_examples/nll_lexical_scope_different/source.rs deleted file mode 100644 index e3022a9..0000000 --- a/src/assets/code_examples/nll_lexical_scope_different/source.rs +++ /dev/null @@ -1,13 +0,0 @@ -fn main() { - let mut x = String::from("Hello"); - let y = &mut x; - world(y); - let z = &mut x; // OK, because y's lifetime has ended (last use was on previous line) - world(z); - x.push_str("!!"); // Also OK, because y and z's lifetimes have ended - println!("{}", x); -} - -fn world(s : &mut String) { - s.push_str(", world"); -} \ No newline at end of file diff --git a/src/assets/code_examples/nll_lexical_scope_different/vis_code.svg b/src/assets/code_examples/nll_lexical_scope_different/vis_code.svg deleted file mode 100644 index e2be808..0000000 --- a/src/assets/code_examples/nll_lexical_scope_different/vis_code.svg +++ /dev/null @@ -1,187 +0,0 @@ - - - examples/nll_lexical_scope_different/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let mut x = String::from("Hello"); - 3 let y = &mut x; - 4 world(y); - 5 let z = &mut x; // OK, because y's lifetime has ended (last use was on previous line) - 6 world(z); - 7 x.push_str("!!"); // Also OK, because y and z's lifetimes have ended - 8 println!("{}", x); - 9 } - 10 - 11 fn world(s : &mut String) { - 12 s.push_str(", world"); - 13 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/nll_lexical_scope_different/vis_timeline.svg b/src/assets/code_examples/nll_lexical_scope_different/vis_timeline.svg deleted file mode 100644 index b3813de..0000000 --- a/src/assets/code_examples/nll_lexical_scope_different/vis_timeline.svg +++ /dev/null @@ -1,362 +0,0 @@ - - - examples/nll_lexical_scope_different/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y|*y - z|*z - s|*s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/printing/source.rs b/src/assets/code_examples/printing/source.rs deleted file mode 100644 index 966217a..0000000 --- a/src/assets/code_examples/printing/source.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let x = 1; - let y = 2; - println!("x = {} and y = {}", x, y); -} \ No newline at end of file diff --git a/src/assets/code_examples/printing/vis_code.svg b/src/assets/code_examples/printing/vis_code.svg deleted file mode 100644 index f6298c2..0000000 --- a/src/assets/code_examples/printing/vis_code.svg +++ /dev/null @@ -1,179 +0,0 @@ - - - examples/printing/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = 1; - 3 let y = 2; - 4 println!("x = {} and y = {}", x, y); - 5 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/printing/vis_timeline.svg b/src/assets/code_examples/printing/vis_timeline.svg deleted file mode 100644 index a6633af..0000000 --- a/src/assets/code_examples/printing/vis_timeline.svg +++ /dev/null @@ -1,282 +0,0 @@ - - - examples/printing/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/string_from/source.rs b/src/assets/code_examples/string_from/source.rs deleted file mode 100644 index eee0975..0000000 --- a/src/assets/code_examples/string_from/source.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - let s = String::from("hello"); -} \ No newline at end of file diff --git a/src/assets/code_examples/string_from/vis_code.svg b/src/assets/code_examples/string_from/vis_code.svg deleted file mode 100644 index 2b4ee74..0000000 --- a/src/assets/code_examples/string_from/vis_code.svg +++ /dev/null @@ -1,177 +0,0 @@ - - - examples/string_from/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - fn main() { - let s = String::from("hello"); - } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/string_from/vis_timeline.svg b/src/assets/code_examples/string_from/vis_timeline.svg deleted file mode 100644 index 12a08b7..0000000 --- a/src/assets/code_examples/string_from/vis_timeline.svg +++ /dev/null @@ -1,223 +0,0 @@ - - - examples/string_from/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - s - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/string_from_move_print/source.rs b/src/assets/code_examples/string_from_move_print/source.rs deleted file mode 100644 index 5717115..0000000 --- a/src/assets/code_examples/string_from_move_print/source.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let x = String::from("hello"); - let y = x; - println!("{}", y); -} \ No newline at end of file diff --git a/src/assets/code_examples/string_from_move_print/vis_code.svg b/src/assets/code_examples/string_from_move_print/vis_code.svg deleted file mode 100644 index d569627..0000000 --- a/src/assets/code_examples/string_from_move_print/vis_code.svg +++ /dev/null @@ -1,179 +0,0 @@ - - - examples/string_from_move_print/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 let y = x; - 4 println!("{}", y); - 5 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/string_from_move_print/vis_timeline.svg b/src/assets/code_examples/string_from_move_print/vis_timeline.svg deleted file mode 100644 index ee67a43..0000000 --- a/src/assets/code_examples/string_from_move_print/vis_timeline.svg +++ /dev/null @@ -1,275 +0,0 @@ - - - examples/string_from_move_print/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/string_from_print/source.rs b/src/assets/code_examples/string_from_print/source.rs deleted file mode 100644 index 736a0c1..0000000 --- a/src/assets/code_examples/string_from_print/source.rs +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - let s = String::from("hello"); - println!("{}", s); -} \ No newline at end of file diff --git a/src/assets/code_examples/string_from_print/vis_code.svg b/src/assets/code_examples/string_from_print/vis_code.svg deleted file mode 100644 index 273becb..0000000 --- a/src/assets/code_examples/string_from_print/vis_code.svg +++ /dev/null @@ -1,178 +0,0 @@ - - - examples/string_from_print/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let s = String::from("hello"); - 3 println!("{}", s); - 4 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/string_from_print/vis_timeline.svg b/src/assets/code_examples/string_from_print/vis_timeline.svg deleted file mode 100644 index 0388958..0000000 --- a/src/assets/code_examples/string_from_print/vis_timeline.svg +++ /dev/null @@ -1,261 +0,0 @@ - - - examples/string_from_print/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_lifetime/source.rs b/src/assets/code_examples/struct_lifetime/source.rs deleted file mode 100644 index 13d61ac..0000000 --- a/src/assets/code_examples/struct_lifetime/source.rs +++ /dev/null @@ -1,17 +0,0 @@ -struct Excerpt<'a> { - p: &'a str, -} - -fn some_function() { - let n = String::from("Ok. I'm fine."); - let first = n.split('.').next().expect("Could not find a '.'"); - let i = Excerpt { - p: first, - }; - // 'i' cannot be returned be returned - // because the struct outlives 'n' -} - -fn main() { - some_function(); -} \ No newline at end of file diff --git a/src/assets/code_examples/struct_lifetime/vis_code.svg b/src/assets/code_examples/struct_lifetime/vis_code.svg deleted file mode 100644 index a0f7f71..0000000 --- a/src/assets/code_examples/struct_lifetime/vis_code.svg +++ /dev/null @@ -1,192 +0,0 @@ - - - examples/struct_lifetime/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 struct Excerpt<'a> { - 2 p: &'a str, - 3 } - 4 - 5 fn some_function() { - 6 let n = String::from("Ok. I'm fine."); - 7 let first = n.split('.').next().expect("Could not find a '.'"); - 8 let i = Excerpt { - 9 p: first, - 10 }; - 11 - 12 // 'i' cannot be returned be returned - 13 // because the struct outlives 'n' - 14 } - 15 - 16 fn main() { - 17 some_function(); - 18 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_lifetime/vis_timeline.svg b/src/assets/code_examples/struct_lifetime/vis_timeline.svg deleted file mode 100644 index 606c7da..0000000 --- a/src/assets/code_examples/struct_lifetime/vis_timeline.svg +++ /dev/null @@ -1,341 +0,0 @@ - - - examples/struct_lifetime/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - first|*first - n|*n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - i - - - - - - - - - - - - - - - - - - - i.p - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_rect/source.rs b/src/assets/code_examples/struct_rect/source.rs deleted file mode 100644 index 06c34e6..0000000 --- a/src/assets/code_examples/struct_rect/source.rs +++ /dev/null @@ -1,22 +0,0 @@ -struct Rect { - w: u32, - h: u32, -} - -fn main() { - let r = Rect { - w: 30, - h: 50, - }; - - println!( - "The area of the rectangle is {} square pixels.", - area(&r) - ); - - println!("The height of that is {}.", r.h); -} - -fn area(rect: &Rect) -> u32 { - rect.w * rect.h -} \ No newline at end of file diff --git a/src/assets/code_examples/struct_rect/vis_code.svg b/src/assets/code_examples/struct_rect/vis_code.svg deleted file mode 100644 index eec3b7a..0000000 --- a/src/assets/code_examples/struct_rect/vis_code.svg +++ /dev/null @@ -1,196 +0,0 @@ - - - examples/struct_rect/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 struct Rect { - 2 w: u32, - 3 h: u32, - 4 } - 5 - 6 fn main() { - 7 let r = Rect { - 8 w: 30, - 9 h: 50, - 10 }; - 11 - 12 println!( - 13 "The area of the rectangle is {} square pixels.", - 14 area(&r) - 15 ); - 16 - 17 println!("The height of that is {}.", r.h); - 18 } - 19 - 20 fn area(rect: &Rect) -> u32 { - 21 rect.w * rect.h - 22 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_rect/vis_timeline.svg b/src/assets/code_examples/struct_rect/vis_timeline.svg deleted file mode 100644 index 5d2a14f..0000000 --- a/src/assets/code_examples/struct_rect/vis_timeline.svg +++ /dev/null @@ -1,336 +0,0 @@ - - - examples/struct_rect/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - rect|*rect - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - r - - - - - - - - - - - - - - - - - - - - - - - - r.w - r.h - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_rect2/source.rs b/src/assets/code_examples/struct_rect2/source.rs deleted file mode 100644 index 77ca4f4..0000000 --- a/src/assets/code_examples/struct_rect2/source.rs +++ /dev/null @@ -1,26 +0,0 @@ -struct Rectangle { - width: u32, - height: u32, -} - -impl Rectangle { - fn area(&self) -> u32 { - self.width * self.height - } -} - -fn print_area(rect: &Rectangle) { - println!( - "The area of the rectangle is {} square pixels.", - rect.area() // dot even though it's actually a reference - ); -} - -fn main() { - let r = Rectangle { - width: 30, - height: 50, - }; - - print_area(&r); -} \ No newline at end of file diff --git a/src/assets/code_examples/struct_rect2/vis_code.svg b/src/assets/code_examples/struct_rect2/vis_code.svg deleted file mode 100644 index 929ae96..0000000 --- a/src/assets/code_examples/struct_rect2/vis_code.svg +++ /dev/null @@ -1,200 +0,0 @@ - - - examples/struct_rect2/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 struct Rectangle { - 2 width: u32, - 3 height: u32, - 4 } - 5 - 6 impl Rectangle { - 7 fn area(&self) -> u32 { - 8 self.width * self.height - 9 } - 10 } - 11 - 12 fn print_area(rect: &Rectangle) -> u32 { - 13 println!( - 14 "The area of the rectangle is {} square pixels.", - 15 rect.area() // dot even though it's actually a reference - 16 ); - 17 } - 18 - 19 fn main() { - 20 let r = Rectangle { - 21 width: 30, - 22 height: 50, - 23 }; - 24 - 25 print_area(&r); - 26 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_rect2/vis_timeline.svg b/src/assets/code_examples/struct_rect2/vis_timeline.svg deleted file mode 100644 index 825f859..0000000 --- a/src/assets/code_examples/struct_rect2/vis_timeline.svg +++ /dev/null @@ -1,347 +0,0 @@ - - - examples/struct_rect2/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - rect|*rect - self|*self - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - r - - - - - - - - - - - - - - - - - - - - - - - - r.width - r.height - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_string/source.rs b/src/assets/code_examples/struct_string/source.rs deleted file mode 100644 index 3238da1..0000000 --- a/src/assets/code_examples/struct_string/source.rs +++ /dev/null @@ -1,11 +0,0 @@ -struct Foo { - x: i32, - y: String, -} - -fn main() { - let _y = String :: from("bar"); - let f = Foo { x: 5, y: _y }; - println!("{}", f.x); - println!("{}", f.y); -} \ No newline at end of file diff --git a/src/assets/code_examples/struct_string/vis_code.svg b/src/assets/code_examples/struct_string/vis_code.svg deleted file mode 100644 index 27d7dec..0000000 --- a/src/assets/code_examples/struct_string/vis_code.svg +++ /dev/null @@ -1,185 +0,0 @@ - - - examples/struct_string/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 struct Foo { - 2 x: i32, - 3 y: String, - 4 } - 5 - 6 fn main() { - 7 let _y = String::from("bar"); - 8 let f = Foo { x: 5, y: _y }; - 9 println!("{}", f.x); - 10 println!("{}", f.y); - 11 } - - - - \ No newline at end of file diff --git a/src/assets/code_examples/struct_string/vis_timeline.svg b/src/assets/code_examples/struct_string/vis_timeline.svg deleted file mode 100644 index 71e2e52..0000000 --- a/src/assets/code_examples/struct_string/vis_timeline.svg +++ /dev/null @@ -1,338 +0,0 @@ - - - examples/struct_string/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - _y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - f - - - - - - - - - - - - - - - - - - - f.x - f.y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/img/0.svg b/src/assets/img/0.svg deleted file mode 100644 index 55daf77..0000000 --- a/src/assets/img/0.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/img/1.svg b/src/assets/img/1.svg deleted file mode 100644 index c7d444b..0000000 --- a/src/assets/img/1.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/img/2.svg b/src/assets/img/2.svg deleted file mode 100644 index 0482578..0000000 --- a/src/assets/img/2.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/img/3.svg b/src/assets/img/3.svg deleted file mode 100644 index b824af3..0000000 --- a/src/assets/img/3.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/img/4.svg b/src/assets/img/4.svg deleted file mode 100644 index 00aea1f..0000000 --- a/src/assets/img/4.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/img/5.svg b/src/assets/img/5.svg deleted file mode 100644 index 60d7447..0000000 --- a/src/assets/img/5.svg +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/assets/img/placeholder.svg b/src/assets/img/placeholder.svg deleted file mode 100644 index fd5c02e..0000000 --- a/src/assets/img/placeholder.svg +++ /dev/null @@ -1,18 +0,0 @@ - - \ No newline at end of file diff --git a/src/assets/modified_examples/func_take_ownership/source.rs b/src/assets/modified_examples/func_take_ownership/source.rs deleted file mode 100644 index cdea2a0..0000000 --- a/src/assets/modified_examples/func_take_ownership/source.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - let s = String::from("hello"); - takes_ownership(s); - // println!("{}", s) // won't compile if added -} - -fn takes_ownership(some_string: String) { - println!("{}", some_string); -} \ No newline at end of file diff --git a/src/assets/modified_examples/func_take_ownership/vis_code.svg b/src/assets/modified_examples/func_take_ownership/vis_code.svg deleted file mode 100644 index f10aa46..0000000 --- a/src/assets/modified_examples/func_take_ownership/vis_code.svg +++ /dev/null @@ -1,183 +0,0 @@ - - - examples/func_take_ownership/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let s = String::from("hello"); - 3 takes_ownership(s); - 4 // println!("{}", s) // won't compile if added - 5 } - 6 - 7 fn takes_ownership(some_string: String) { - 8 println!("{}", some_string); - 9 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/func_take_ownership/vis_timeline.svg b/src/assets/modified_examples/func_take_ownership/vis_timeline.svg deleted file mode 100644 index 23490a1..0000000 --- a/src/assets/modified_examples/func_take_ownership/vis_timeline.svg +++ /dev/null @@ -1,276 +0,0 @@ - - - examples/func_take_ownership/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - some_string - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/func_take_return_ownership/source.rs b/src/assets/modified_examples/func_take_return_ownership/source.rs deleted file mode 100644 index 46846cc..0000000 --- a/src/assets/modified_examples/func_take_return_ownership/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn take_and_return_ownership(some_string : String) -> String { - println!("{}", some_string); - some_string -} - -fn main() { - let mut s = String::from("hello"); - s = take_and_return_ownership(s); - println!("{}", s); // OK -} \ No newline at end of file diff --git a/src/assets/modified_examples/func_take_return_ownership/vis_code.svg b/src/assets/modified_examples/func_take_return_ownership/vis_code.svg deleted file mode 100644 index 8da896f..0000000 --- a/src/assets/modified_examples/func_take_return_ownership/vis_code.svg +++ /dev/null @@ -1,184 +0,0 @@ - - - examples/func_take_return_ownership/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn take_and_return_ownership(some_string : String) -> String { - 2 println!("{}", some_string); - 3 some_string - 4 } - 5 - 6 fn main() { - 7 let mut s = String::from("hello"); - 8 s = take_and_return_ownership(s); - 9 println!("{}", s); // OK - 10 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/func_take_return_ownership/vis_timeline.svg b/src/assets/modified_examples/func_take_return_ownership/vis_timeline.svg deleted file mode 100644 index ce6acb3..0000000 --- a/src/assets/modified_examples/func_take_return_ownership/vis_timeline.svg +++ /dev/null @@ -1,300 +0,0 @@ - - - examples/func_take_return_ownership/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - some_string - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/hatra2/source.rs b/src/assets/modified_examples/hatra2/source.rs deleted file mode 100644 index 61992d0..0000000 --- a/src/assets/modified_examples/hatra2/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn main(){ - let mut s = String::from("hello"); - - let r1 = &s; - let r2 = &s; - assert!(compare_strings(r1, r2)); - - let r3 = &mut s; - clear_string(r3); -} \ No newline at end of file diff --git a/src/assets/modified_examples/hatra2/vis_code.svg b/src/assets/modified_examples/hatra2/vis_code.svg deleted file mode 100644 index 8021402..0000000 --- a/src/assets/modified_examples/hatra2/vis_code.svg +++ /dev/null @@ -1,185 +0,0 @@ - - - examples/hatra2/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main(){ - 2 let mut s = String::from("hello"); - 3 - 4 let r1 = &s; - 5 let r2 = &s; - 6 assert!(compare_strings(r1, r2)); - 7 - 8 - 9 let r3 = &mut s; - 10 clear_string(r3); - 11 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/hatra2/vis_timeline.svg b/src/assets/modified_examples/hatra2/vis_timeline.svg deleted file mode 100644 index 0a6ed68..0000000 --- a/src/assets/modified_examples/hatra2/vis_timeline.svg +++ /dev/null @@ -1,365 +0,0 @@ - - - examples/hatra2/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - r1|*r1 - r2|*r2 - r3|*r3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/immutable_borrow/source.rs b/src/assets/modified_examples/immutable_borrow/source.rs deleted file mode 100644 index b05ff3c..0000000 --- a/src/assets/modified_examples/immutable_borrow/source.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - let x = String::from("hello"); - f(&x); - println!("{}", x); -} - -fn f(s : &String) { - println!("{}", *s); -} \ No newline at end of file diff --git a/src/assets/modified_examples/immutable_borrow/vis_code.svg b/src/assets/modified_examples/immutable_borrow/vis_code.svg deleted file mode 100644 index dc90f9e..0000000 --- a/src/assets/modified_examples/immutable_borrow/vis_code.svg +++ /dev/null @@ -1,183 +0,0 @@ - - - examples/immutable_borrow/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 f(&x); - 4 println!("{}", x); - 5 } - 6 - 7 fn f(s : &String) { - 8 println!("{}", *s); - 9 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/immutable_borrow/vis_timeline.svg b/src/assets/modified_examples/immutable_borrow/vis_timeline.svg deleted file mode 100644 index cc96572..0000000 --- a/src/assets/modified_examples/immutable_borrow/vis_timeline.svg +++ /dev/null @@ -1,298 +0,0 @@ - - - examples/immutable_borrow/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - s|*s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/immutable_borrow_method_call/source.rs b/src/assets/modified_examples/immutable_borrow_method_call/source.rs deleted file mode 100644 index fb1ade4..0000000 --- a/src/assets/modified_examples/immutable_borrow_method_call/source.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - let s = String::from("hello"); - let len1 = String::len(&s); - let len2 = s.len(); // shorthand for the above - println!("len1 = {} = len2 = {}", len1, len2); -} \ No newline at end of file diff --git a/src/assets/modified_examples/immutable_borrow_method_call/vis_code.svg b/src/assets/modified_examples/immutable_borrow_method_call/vis_code.svg deleted file mode 100644 index d29b266..0000000 --- a/src/assets/modified_examples/immutable_borrow_method_call/vis_code.svg +++ /dev/null @@ -1,180 +0,0 @@ - - - examples/immutable_borrow_method_call/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let s = String::from("hello"); - 3 let len1 = String::len(&s); - 4 let len2 = s.len(); // shorthand for the above - 5 println!("len1 = {} = len2 = {}", len1, len2); - 6 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/immutable_borrow_method_call/vis_timeline.svg b/src/assets/modified_examples/immutable_borrow_method_call/vis_timeline.svg deleted file mode 100644 index 5c6fbbd..0000000 --- a/src/assets/modified_examples/immutable_borrow_method_call/vis_timeline.svg +++ /dev/null @@ -1,324 +0,0 @@ - - - examples/immutable_borrow_method_call/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s - len1 - len2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/move_assignment/source.rs b/src/assets/modified_examples/move_assignment/source.rs deleted file mode 100644 index b5b8d61..0000000 --- a/src/assets/modified_examples/move_assignment/source.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - let x = String::from("hello"); - let mut y = String::from("test"); - y = x; -} \ No newline at end of file diff --git a/src/assets/modified_examples/move_assignment/vis_code.svg b/src/assets/modified_examples/move_assignment/vis_code.svg deleted file mode 100644 index 964718f..0000000 --- a/src/assets/modified_examples/move_assignment/vis_code.svg +++ /dev/null @@ -1,179 +0,0 @@ - - - examples/move_assignment/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 let mut y = String::from("test"); - 4 y = x; - 5 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/move_assignment/vis_timeline.svg b/src/assets/modified_examples/move_assignment/vis_timeline.svg deleted file mode 100644 index da51429..0000000 --- a/src/assets/modified_examples/move_assignment/vis_timeline.svg +++ /dev/null @@ -1,270 +0,0 @@ - - - examples/move_assignment/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/move_func_return/source.rs b/src/assets/modified_examples/move_func_return/source.rs deleted file mode 100644 index 6798b2b..0000000 --- a/src/assets/modified_examples/move_func_return/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn f() { - let x = String::from("hello"); - // ... - x -} - -fn main() { - let s = f(); - println!("{}", s); -} \ No newline at end of file diff --git a/src/assets/modified_examples/move_func_return/vis_code.svg b/src/assets/modified_examples/move_func_return/vis_code.svg deleted file mode 100644 index afe7c7d..0000000 --- a/src/assets/modified_examples/move_func_return/vis_code.svg +++ /dev/null @@ -1,184 +0,0 @@ - - - examples/move_func_return/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn f() { - 2 let x = String::from("hello"); - 3 // ... - 4 x - 5 } - 6 - 7 fn main() { - 8 let s = f(); - 9 println!("{}", s); - 10 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/move_func_return/vis_timeline.svg b/src/assets/modified_examples/move_func_return/vis_timeline.svg deleted file mode 100644 index 0ce8adb..0000000 --- a/src/assets/modified_examples/move_func_return/vis_timeline.svg +++ /dev/null @@ -1,276 +0,0 @@ - - - examples/move_func_return/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/multiple_immutable_borrow/source.rs b/src/assets/modified_examples/multiple_immutable_borrow/source.rs deleted file mode 100644 index 87f645a..0000000 --- a/src/assets/modified_examples/multiple_immutable_borrow/source.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn main() { - let x = String::from("hello"); - let y = &x; - let z = &x; - f(y, z); -} - -fn f(s1 : &String, s2 : &String) { - println!("{} and {}", s1, s2); -} \ No newline at end of file diff --git a/src/assets/modified_examples/multiple_immutable_borrow/vis_code.svg b/src/assets/modified_examples/multiple_immutable_borrow/vis_code.svg deleted file mode 100644 index 1996a2e..0000000 --- a/src/assets/modified_examples/multiple_immutable_borrow/vis_code.svg +++ /dev/null @@ -1,185 +0,0 @@ - - - examples/multiple_immutable_borrow/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let x = String::from("hello"); - 3 let y = &x; - 4 let z = &x; - 5 f(y, z); - 6 - 7 } - 8 - 9 fn f(s1 : &String, s2 : &String) { - 10 println!("{} and {}", s1, s2); - 11 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/multiple_immutable_borrow/vis_timeline.svg b/src/assets/modified_examples/multiple_immutable_borrow/vis_timeline.svg deleted file mode 100644 index 9f4639c..0000000 --- a/src/assets/modified_examples/multiple_immutable_borrow/vis_timeline.svg +++ /dev/null @@ -1,378 +0,0 @@ - - - examples/multiple_immutable_borrow/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y|*y - z|*z - s1|*s1 - s2|*s2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/mutable_borrow/source.rs b/src/assets/modified_examples/mutable_borrow/source.rs deleted file mode 100644 index f49b480..0000000 --- a/src/assets/modified_examples/mutable_borrow/source.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - let mut x = String::from("Hello"); - world(&mut x); - println!("{}", x); -} - -fn world(s : &mut String) { - s.push_str(", world"); -} \ No newline at end of file diff --git a/src/assets/modified_examples/mutable_borrow/vis_code.svg b/src/assets/modified_examples/mutable_borrow/vis_code.svg deleted file mode 100644 index 631f094..0000000 --- a/src/assets/modified_examples/mutable_borrow/vis_code.svg +++ /dev/null @@ -1,183 +0,0 @@ - - - examples/mutable_borrow/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let mut x = String::from("hello"); - 3 world(&mut &x); - 4 println!("{}", x); - 5 } - 6 - 7 fn world(s : &mut String) { - 8 s.push_str(", world"); - 9 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/mutable_borrow/vis_timeline.svg b/src/assets/modified_examples/mutable_borrow/vis_timeline.svg deleted file mode 100644 index 0e2476f..0000000 --- a/src/assets/modified_examples/mutable_borrow/vis_timeline.svg +++ /dev/null @@ -1,292 +0,0 @@ - - - examples/mutable_borrow/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - s|*s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/mutable_borrow_method_call/source.rs b/src/assets/modified_examples/mutable_borrow_method_call/source.rs deleted file mode 100644 index 4a8cfd2..0000000 --- a/src/assets/modified_examples/mutable_borrow_method_call/source.rs +++ /dev/null @@ -1,7 +0,0 @@ -fn main() { - let mut s1 = String::from("Hello"); - let s2 = String::from(", world"); - String::push_str(&mut s1, &s2); - s1.push_str(&s2); // shorthand for the above - println!("{}", s1); // prints "Hello, world, world" -} \ No newline at end of file diff --git a/src/assets/modified_examples/mutable_borrow_method_call/vis_code.svg b/src/assets/modified_examples/mutable_borrow_method_call/vis_code.svg deleted file mode 100644 index 992ad01..0000000 --- a/src/assets/modified_examples/mutable_borrow_method_call/vis_code.svg +++ /dev/null @@ -1,181 +0,0 @@ - - - examples/mutable_borrow_method_call/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let mut s1 = String::from("hello"); - 3 let s2 = String::from(", world"); - 4 String::push_str(&mut s1, &s2); - 5 s1.push_str(&s2); // shorthand for the above - 6 println!("{}", s1); // prints "Hello, world, world" - 7 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/mutable_borrow_method_call/vis_timeline.svg b/src/assets/modified_examples/mutable_borrow_method_call/vis_timeline.svg deleted file mode 100644 index 7b7531b..0000000 --- a/src/assets/modified_examples/mutable_borrow_method_call/vis_timeline.svg +++ /dev/null @@ -1,319 +0,0 @@ - - - examples/mutable_borrow_method_call/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - s1 - s2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/nll_lexical_scope_different/source.rs b/src/assets/modified_examples/nll_lexical_scope_different/source.rs deleted file mode 100644 index e3022a9..0000000 --- a/src/assets/modified_examples/nll_lexical_scope_different/source.rs +++ /dev/null @@ -1,13 +0,0 @@ -fn main() { - let mut x = String::from("Hello"); - let y = &mut x; - world(y); - let z = &mut x; // OK, because y's lifetime has ended (last use was on previous line) - world(z); - x.push_str("!!"); // Also OK, because y and z's lifetimes have ended - println!("{}", x); -} - -fn world(s : &mut String) { - s.push_str(", world"); -} \ No newline at end of file diff --git a/src/assets/modified_examples/nll_lexical_scope_different/vis_code.svg b/src/assets/modified_examples/nll_lexical_scope_different/vis_code.svg deleted file mode 100644 index e2be808..0000000 --- a/src/assets/modified_examples/nll_lexical_scope_different/vis_code.svg +++ /dev/null @@ -1,187 +0,0 @@ - - - examples/nll_lexical_scope_different/input/ - - - - - - - Hover over timeline events (dots), states (vertical lines), - and actions (arrows) for extra information. - - - - 1 fn main() { - 2 let mut x = String::from("Hello"); - 3 let y = &mut x; - 4 world(y); - 5 let z = &mut x; // OK, because y's lifetime has ended (last use was on previous line) - 6 world(z); - 7 x.push_str("!!"); // Also OK, because y and z's lifetimes have ended - 8 println!("{}", x); - 9 } - 10 - 11 fn world(s : &mut String) { - 12 s.push_str(", world"); - 13 } - - - - \ No newline at end of file diff --git a/src/assets/modified_examples/nll_lexical_scope_different/vis_timeline.svg b/src/assets/modified_examples/nll_lexical_scope_different/vis_timeline.svg deleted file mode 100644 index b3813de..0000000 --- a/src/assets/modified_examples/nll_lexical_scope_different/vis_timeline.svg +++ /dev/null @@ -1,362 +0,0 @@ - - - examples/nll_lexical_scope_different/input/ - - - - - - - f - - - - - - - - - - - - - - - - - - - - - - - - - - - x - y|*y - z|*z - s|*s - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/borrowing.md b/src/borrowing.md index 4441ef0..f8e0011 100644 --- a/src/borrowing.md +++ b/src/borrowing.md @@ -13,10 +13,18 @@ example, `take_and_return_ownership` below takes ownership of a string resource and returns ownership of that exact same resource. The caller, `main`, assigns the returned resource to the same variable, `s`. -
- - -
+```rv +fn take_and_return_ownership(some_string : String) -> String { + println!("{}", some_string); + some_string +} + +fn main() { + let mut s = String::from("hello"); + s = take_and_return_ownership(s); + println!("{}", s); // OK +} +``` This code prints `hello` twice. @@ -54,11 +62,17 @@ takes a reference, it is only *borrowing* access to the resource that the reference points to. It does not need to explicitly return the resource because it does not own it. Rust knows that the borrow does not outlive the owner because the borrow is no longer accessible after `f` returns. +```rv +fn main() { + let x = String::from("hello"); + f(&x); + println!("{}", x); +} -
- - -
+fn f(s : &String) { + println!("{}", *s); +} +``` This code prints `hello` twice. @@ -73,10 +87,14 @@ take their arguments by reference. You can call a method explicitly with a reference, e.g. `String::len(&s)`. As shorthand, you can use dot notation to call a method, e.g. `s.len()`. This implicitly takes a reference to `s`. -
- - -
+```rv +fn main() { + let s = String::from("hello"); + let len1 = String::len(&s); + let len2 = s.len(); // shorthand for the above + println!("len1 = {} = len2 = {}", len1, len2); +} +``` This code prints `len1 = 5 = len2 = 5`. @@ -86,10 +104,18 @@ reason, immutable borrows are also sometimes called shared borrows: each immutable reference shares access to the resource with the owner and with any other immutable references that might be live. -
- - -
+```rv +fn main() { + let x = String::from("hello"); + let y = &x; + let z = &x; + f(y, z); +} + +fn f(s1 : &String, s2 : &String) { + println!("{} and {}", s1, s2); +} +``` This code prints `hello and hello`. @@ -116,10 +142,15 @@ the `String::push_str` method, and then using the equivalent shorthand method call syntax. In both cases, the method takes a *mutable reference* to `s1`, written explicitly `&mut s1`. -
- - -
+```rv +fn main() { + let mut s1 = String::from("Hello"); + let s2 = String::from(", world"); + String::push_str(&mut s1, &s2); + s1.push_str(&s2); // shorthand for the above + println!("{}", s1); // prints "Hello, world, world" +} +``` This code prints `Hello, world, world`. @@ -202,9 +233,20 @@ there remain future uses of the borrow. A borrow dies as soon it is no longer needed. So the following code works, even though there are two mutable borrows in the same scope: -
- - -
+```rv +fn main() { + let mut x = String::from("Hello"); + let y = &mut x; + world(y); + let z = &mut x; // OK, because y's lifetime has ended (last use was on previous line) + world(z); + x.push_str("!!"); // Also OK, because y and z's lifetimes have ended + println!("{}", x); +} + +fn world(s : &mut String) { + s.push_str(", world"); +} +``` This code prints `Hello, world, world!!`. \ No newline at end of file diff --git a/src/motivation.md b/src/motivation.md index 85b14d0..ca75423 100644 --- a/src/motivation.md +++ b/src/motivation.md @@ -34,10 +34,26 @@ Hover over the different components of the visualization to see explanations. Don't worry yet about what is going on in detail—these concepts will be explained in this tutorial. -
- - -
+```rv +fn compare_strings(s1: &String, s2: &String) -> bool{ + *s1 == *s2 +} + +fn clear_string(s3: & mut String) { + s3.clear(); +} + +fn main(){ + let mut s = String::from("hello"); + + let r1 = &s; + let r2 = &s; + compare_strings(r1, r2); // can't use assert macro (desugared to an if expr) + + let r3 = &mut s; + clear_string(r3); +} +``` ## Research Disclosure diff --git a/src/ownership.md b/src/ownership.md index 9d0beb3..43a45f8 100644 --- a/src/ownership.md +++ b/src/ownership.md @@ -37,10 +37,12 @@ For example, heap-allocated strings, of type `String`, are managed by Rust's own Consider the following example, which constructs a heap-allocated string and prints it out. -
- - -
+```rv +fn main() { + let s = String::from("hello"); + println!("{}", s); +} +``` This code prints `hello`. @@ -110,22 +112,32 @@ then you can see by hovering over the visualization that the resource is dropped at the end of `y`'s scope rather than at the end of `x`'s scope. -
- - -
+```rv +fn main() { + let x = String::from("hello"); + let z = { + let y = x; + println!("{}", y); + // ... + }; + println!("Hello, world!"); +} +``` This code prints `hello` on one line and `Hello, world!` on the next. ### Assignment -As with binding, ownership can be moved by assignment to a mutable variable, +As with binding, ownership can be moved by assignment to a mutable variable,cd e.g. `y` in the following example. -
- - -
+```rv +fn main() { + let x = String::from("hello"); + let mut y = String::from("test"); + y = x; +} +``` When `y` acquires ownership over `x`'s resource on Line 4, the resource it previously acquired (on Line 3) no longer has an owner, so it is dropped. @@ -138,10 +150,17 @@ below we see that ownership of the string resource in `main` is moved from `s` to the `takes_ownership` function. Consequently, when `s` goes out of scope at the end of `main`, there is no owned string resource to be dropped. -
- - -
+```rv +fn main() { + let s = String::from("hello"); + takes_ownership(s); + // println!("{}", s) // won't compile if added +} + +fn takes_ownership(some_string: String) { + println!("{}", some_string); +} +``` This code prints `hello`. @@ -162,9 +181,17 @@ owner, `s`, goes out of scope at the end of `main`. (If the `String` were dropped at the end of `f`, there would be a use-after-free bug in `main` on Line 9!) -
- - -
+```rv +fn f() -> String { + let x = String::from("hello"); + // ... + x +} + +fn main() { + let s = f(); + println!("{}", s); +} +``` This code prints `hello`. diff --git a/src/rust-basics.md b/src/rust-basics.md index 2b9bd3b..8eabb8f 100644 --- a/src/rust-basics.md +++ b/src/rust-basics.md @@ -17,7 +17,9 @@ By default, variables are *immutable* in Rust. This means that once a value is bound to the variable, the binding cannot be changed. We use `let` bindings to introduce immutable variables as follows: ```rust -{{#rustdoc_include assets/code_examples/immutable_variable/source.rs}} +fn main() { + let x = 5; +} ``` In this example, we introduce a variable `x` of type `i32` (a 32-bit signed @@ -36,14 +38,20 @@ fn main() { If you want to be able to assign to a variable, it must be marked as *mutable* with `let mut`: ```rust -{{#rustdoc_include assets/code_examples/mutable_variables/source.rs}} +fn main() { + let mut x = 5; + x = 6; //OK +} ``` ## Copies For simple types like integers, binding and assignment creates a copy. For example, we can bind the value `5` to `x` and then bind `y` with a copy of `x`: ```rust -{{#rustdoc_include assets/code_examples/copy/source.rs}} +fn main() { + let x = 5; + let y = x; +} ``` Copying occurs only for simple types like `i32` and other types that @@ -58,7 +66,13 @@ Besides `main`, we can define additional functions. In the following example, we define a function called `plus_one` which takes an `i32` as input and returns an `i32` value that is one more than the input: ```rust -{{#rustdoc_include assets/code_examples/function/source.rs}} +fn main() { + let six = plus_one(5); +} + +fn plus_one(x: i32) -> i32 { + x + 1 +} ``` Notice how there is no explicit return. In Rust, if the last expression in the @@ -78,7 +92,11 @@ character. We can also use curly brackets in the input string of `println!` as a placeholder for subsequent arguments: ```rust -{{#rustdoc_include assets/code_examples/printing/source.rs}} +fn main() { + let x = 1; + let y = 2; + println!("x = {} and y = {}", x, y); +} ``` This prints `x = 1 and y = 2`. diff --git a/src/structs.md b/src/structs.md index dd93b47..d791423 100644 --- a/src/structs.md +++ b/src/structs.md @@ -1,60 +1,129 @@ -### Optional: Structs in Rust - -#### Creating a struct - -To define a struct, we enter the keyword `struct` and name the entire struct. A struct’s name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call *fields*. Here is an example showing a struct that stores information about a user account. - -```rust -struct User { - username: String, - email: String, - sign_in_count: u64, - active: bool, -} -``` - -We create an instance by stating the name of the struct and then add curly brackets containing `key: value` pairs, where the keys are the names of the fields and the values are the data we want to store in those fields. Then we can use dot field to obtain the value in a struct. - -```rust - let mut user1 = User { - email: String::from("someone@example.com"), - username: String::from("someusername123"), - active: true, - sign_in_count: 1, - }; - - user1.email = String::from("anotheremail@example.com"); -``` -Each fields in the struct can be referenced independently. Here's an example of defining a struct, generating an instance of it, letting it interact with functions and referencing field `r.h`. - -
- - -
- -#### Calling a method in a struct - -Struct can also include methods whose definition is given in the `impl` of it. When calling a method or a variable from a struct, we use `object.something()`or ` (&object).something()`, which are the same. No matter it is a `&, &mut, *`or nothing, always use `.` and not need to use `->` because Rust will automatically adds in `&, &mut, *` so `object` matches the signature of the method. - -
- - -
- -#### Ownership of struct data - -When the instance of the struct owns all its fields, i.e. no reference or pointer in the struct, the ownership is basically the same with data outside of a struct. It's also possible for fields of a struct to own resources. Here's an example of the cases where one of the field `y` owns a `string` resouce. - -
- - -
- -When the any of the data members is not owned by the struct, it needs lexical lifetime specified to allow the struct owning a reference of a data resouce. This will ensure that the resource referenced will have the same lifetime as the struct as long as they share the same lexical lifetime label. - -Here is an example of using lifetime annotations `<'a>` in struct definitions to allow reference of string `&p` in a `struct Excerpt`. - -
- - -
+### Optional: Structs in Rust + +#### Creating a struct + +To define a struct, we enter the keyword `struct` and name the entire struct. A struct’s name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call *fields*. Here is an example showing a struct that stores information about a user account. + +```rust +struct User { + username: String, + email: String, + sign_in_count: u64, + active: bool, +} +``` + +We create an instance by stating the name of the struct and then add curly brackets containing `key: value` pairs, where the keys are the names of the fields and the values are the data we want to store in those fields. Then we can use dot field to obtain the value in a struct. + +```rust + let mut user1 = User { + email: String::from("someone@example.com"), + username: String::from("someusername123"), + active: true, + sign_in_count: 1, + }; + + user1.email = String::from("anotheremail@example.com"); +``` +Each fields in the struct can be referenced independently. Here's an example of defining a struct, generating an instance of it, letting it interact with functions and referencing field `r.h`. + +```rv +struct Rect { + w: u32, + h: u32, +} + +fn main() { + let r = Rect { + w: 30, + h: 50, + }; + + println!( + "The area of the rectangle is {} square pixels.", + area(&r) + ); + + println!("The height of that is {}.", r.h); +} + +fn area(rect: &Rect) -> u32 { + rect.w * rect.h +} +``` + +#### Calling a method in a struct + +Struct can also include methods whose definition is given in the `impl` of it. When calling a method or a variable from a struct, we use `object.something()`or ` (&object).something()`, which are the same. No matter it is a `&, &mut, *`or nothing, always use `.` and not need to use `->` because Rust will automatically adds in `&, &mut, *` so `object` matches the signature of the method. + +```rv +struct Rectangle { + width: u32, + height: u32, +} + +impl Rectangle { + fn area(&self) -> u32 { + self.width * self.height + } +} + +fn print_area(rect: &Rectangle) { + println!( + "The area of the rectangle is {} square pixels.", + rect.area() // dot even though it's actually a reference + ); +} + +fn main() { + let r = Rectangle { + width: 30, + height: 50, + }; + + print_area(&r); +} +``` + +#### Ownership of struct data + +When the instance of the struct owns all its fields, i.e. no reference or pointer in the struct, the ownership is basically the same with data outside of a struct. It's also possible for fields of a struct to own resources. Here's an example of the cases where one of the field `y` owns a `string` resouce. + +```rv +struct Foo { + x: i32, + y: String, +} + +fn main() { + let _y = String :: from("bar"); + let f = Foo { x: 5, y: _y }; + println!("{}", f.x); + println!("{}", f.y); +} +``` + +When the any of the data members is not owned by the struct, it needs lexical lifetime specified to allow the struct owning a reference of a data resouce. This will ensure that the resource referenced will have the same lifetime as the struct as long as they share the same lexical lifetime label. + +Here is an example of using lifetime annotations `<'a>` in struct definitions to allow reference of string `&p` in a `struct Excerpt`. + +```rust +struct Excerpt<'a> { + p: &'a str, +} + +fn some_function() { + let n = String::from("Ok. I'm fine."); + let first = n.split('.').next().expect("Could not find a '.'"); + let i = Excerpt { + p: first, + }; + println!("{}", first); + // 'i' cannot be returned be returned + // because the struct outlives 'n' +} + +fn main() { + some_function(); +} +``` \ No newline at end of file diff --git a/theme/book.js b/theme/book.js deleted file mode 100755 index ea1fe4d..0000000 --- a/theme/book.js +++ /dev/null @@ -1,757 +0,0 @@ -"use strict"; - -// Fix back button cache problem -window.onunload = function () {}; - -(function addAnalytics() { - let s = document.createComment(" Global site tag (gtag.js) - Google Analytics for RustViz "); - document.head.append(s); - - s = document.createElement('script'); - s.setAttribute("src", "https://www.googletagmanager.com/gtag/js?id=G-W8JP8LHES3"); - s.async = true; - document.head.append(s); - - s = document.createElement('script'); - s.innerHTML = "window.dataLayer = window.dataLayer || [];\ - function gtag(){dataLayer.push(arguments);}\ - gtag('js', new Date());\ - gtag('config', 'G-W8JP8LHES3');"; - document.head.append(s); - - // page viewed - gtag('event', 'page_view', { - page_path: location.pathname, - page_title: document.title - }); -})(); - -// Global variable, shared between modules -function playpen_text(playpen) { - let code_block = playpen.querySelector("code"); - - if (window.ace && code_block.classList.contains("editable")) { - let editor = window.ace.edit(code_block); - return editor.getValue(); - } else { - return code_block.textContent; - } -} - -function adjust_visualization_size(flexbox) { - /* resize the dimension of the object tag to match the internal svg; this needs to be triggered everytime each panel resizes */ - // compute how wide the text sections should be - let text_width = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--content-max-width')); - let flex_border_size = parseInt('5px'); // this parsing in intentional as a hint for the text_width - - let timeline_doc = flexbox.querySelector('object[class*="tl_panel"]').contentDocument.querySelector('svg'); - let timeline_width = parseInt(timeline_doc.width.baseVal.value); - let desired_height = parseInt(timeline_doc.height.baseVal.value); - let code_panel_doc = flexbox.querySelector('object[class*="code_panel"]').contentDocument.querySelector('svg'); - let code_panel_width = parseInt(code_panel_doc.width.baseVal.value); - - // update the div block that surround them with the new width - // Rule: if the two panels combined are narrower than the main text, simply set to the text width - // Otherwise, do a "center" effect. - var butt = document.getElementsByClassName("buttons")[0]; - let margin = 0; - if (text_width >= timeline_width + code_panel_width) { - flexbox.style.marginLeft = "0px"; - margin = text_width-timeline_width-code_panel_width; - flexbox.style.marginRight = margin + "px"; - butt.setAttribute("style", "position: absolute; right: " + margin + "px;"); - } else { - let wiggle_room = parseInt("3px"); // manually tweak this to prevent subpixel splitting - let margin_shrink = (timeline_width + code_panel_width + flex_border_size + wiggle_room - text_width) / 2; - flexbox.style.marginLeft = -margin_shrink + "px"; - flexbox.style.marginRight = -margin_shrink + "px"; - } - flexbox.style.height = desired_height + "px"; - return margin; -} - -(function codeSnippets() { - function fetch_with_timeout(url, options, timeout = 6000) { - return Promise.race([ - fetch(url, options), - new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout)) - ]); - } - - var playpens = Array.from(document.querySelectorAll(".playpen")); - if (playpens.length > 0) { - fetch_with_timeout("https://play.rust-lang.org/meta/crates", { - headers: { - 'Content-Type': "application/json", - }, - method: 'POST', - mode: 'cors', - }) - .then(response => response.json()) - .then(response => { - // get list of crates available in the rust playground - let playground_crates = response.crates.map(item => item["id"]); - playpens.forEach(block => handle_crate_list_update(block, playground_crates)); - }); - } - - function handle_crate_list_update(playpen_block, playground_crates) { - // update the play buttons after receiving the response - update_play_button(playpen_block, playground_crates); - - // and install on change listener to dynamically update ACE editors - if (window.ace) { - let code_block = playpen_block.querySelector("code"); - if (code_block.classList.contains("editable")) { - let editor = window.ace.edit(code_block); - editor.addEventListener("change", function (e) { - update_play_button(playpen_block, playground_crates); - }); - // add Ctrl-Enter command to execute rust code - editor.commands.addCommand({ - name: "run", - bindKey: { - win: "Ctrl-Enter", - mac: "Ctrl-Enter" - }, - exec: _editor => run_rust_code(playpen_block) - }); - } - } - } - - // updates the visibility of play button based on `no_run` class and - // used crates vs ones available on http://play.rust-lang.org - function update_play_button(pre_block, playground_crates) { - var play_button = pre_block.querySelector(".play-button"); - - // skip if code is `no_run` - if (pre_block.querySelector('code').classList.contains("no_run")) { - play_button.classList.add("hidden"); - return; - } - - // get list of `extern crate`'s from snippet - var txt = playpen_text(pre_block); - var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g; - var snippet_crates = []; - var item; - while (item = re.exec(txt)) { - snippet_crates.push(item[1]); - } - - // check if all used crates are available on play.rust-lang.org - var all_available = snippet_crates.every(function (elem) { - return playground_crates.indexOf(elem) > -1; - }); - - if (all_available) { - play_button.classList.remove("hidden"); - } else { - play_button.classList.add("hidden"); - } - } - - function run_rust_code(code_block) { - var result_block = code_block.querySelector(".result"); - if (!result_block) { - result_block = document.createElement('code'); - result_block.className = 'result hljs language-bash'; - - code_block.append(result_block); - } - - let text = playpen_text(code_block); - let classes = code_block.querySelector('code').classList; - let has_2018 = classes.contains("edition2018"); - let edition = has_2018 ? "2018" : "2015"; - - var params = { - version: "stable", - optimize: "0", - code: text, - edition: edition - }; - - if (text.indexOf("#![feature") !== -1) { - params.version = "nightly"; - } - - result_block.innerText = "Running..."; - - fetch_with_timeout("https://play.rust-lang.org/evaluate.json", { - headers: { - 'Content-Type': "application/json", - }, - method: 'POST', - mode: 'cors', - body: JSON.stringify(params) - }) - .then(response => response.json()) - .then(response => result_block.innerText = response.result) - .catch(error => result_block.innerText = "Playground Communication: " + error.message); - } - - // Syntax highlighting Configuration - hljs.configure({ - tabReplace: ' ', // 4 spaces - languages: [], // Languages used for auto-detection - }); - - if (window.ace) { - // language-rust class needs to be removed for editable - // blocks or highlightjs will capture events - Array - .from(document.querySelectorAll('code.editable')) - .forEach(function (block) { block.classList.remove('language-rust'); }); - - Array - .from(document.querySelectorAll('code:not(.editable)')) - .forEach(function (block) { hljs.highlightBlock(block); }); - } else { - Array - .from(document.querySelectorAll('code')) - .forEach(function (block) { hljs.highlightBlock(block); }); - } - - // Adding the hljs class gives code blocks the color css - // even if highlighting doesn't apply - Array - .from(document.querySelectorAll('code')) - .forEach(function (block) { block.classList.add('hljs'); }); - - Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) { - - var lines = Array.from(block.querySelectorAll('.boring')); - // If no lines were hidden, return - if (!lines.length) { return; } - block.classList.add("hide-boring"); - - var buttons = document.createElement('div'); - buttons.className = 'buttons'; - buttons.innerHTML = ""; - - // add expand button - var pre_block = block.parentNode; - pre_block.insertBefore(buttons, pre_block.firstChild); - - pre_block.querySelector('.buttons').addEventListener('click', function (e) { - if (e.target.classList.contains('fa-expand')) { - e.target.classList.remove('fa-expand'); - e.target.classList.add('fa-compress'); - e.target.title = 'Hide lines'; - e.target.setAttribute('aria-label', e.target.title); - - block.classList.remove('hide-boring'); - } else if (e.target.classList.contains('fa-compress')) { - e.target.classList.remove('fa-compress'); - e.target.classList.add('fa-expand'); - e.target.title = 'Show hidden lines'; - e.target.setAttribute('aria-label', e.target.title); - - block.classList.add('hide-boring'); - } - }); - }); - - // Insert toggle visualization button, ADDED BY marcelo-a - Array.from(document.querySelectorAll("pre code")).forEach(function (block) { - // only add button if there is a visualization available - let code_ok = (!block.classList.contains("no_run") && !block.classList.contains("does_not_compile") - && !block.classList.contains("language-text")); - if (code_ok) { - // assumes block.parentNode.parentNode = pre with child "playpen" - // assumes nextElementSibling is valid element - let contains_vis = block.parentNode.parentNode.nextElementSibling.classList.contains('vis_block'); - // only add toggle button if visualization wsa properly labeled - if (!contains_vis) return; - - // search section/block for other buttons - var pre_block = block.parentNode; - let buttons = pre_block.querySelector(".buttons"); - if (!buttons) { - buttons = document.createElement('div'); - buttons.className = 'buttons'; - pre_block.insertBefore(buttons, pre_block.firstChild); - } - - // create button element - let toggleButton = document.createElement('button'); - toggleButton.className = 'fa fa-toggle-off toggle-button'; - toggleButton.title = 'Toggle visualization'; - toggleButton.setAttribute('aria-label', toggleButton.title); - - buttons.insertBefore(toggleButton, buttons.firstChild); - block.style.display = 'block'; // initialize display to original code - - var resize_done = false; - var butt = document.getElementsByClassName("buttons")[0]; - var resized_butt_right = 0; - pre_block.querySelector('.buttons').addEventListener('click', function (e) { - if (e.target.classList.contains('fa-toggle-on')) { - // on button click, show visualization and hide code - e.target.classList.remove('fa-toggle-on'); - e.target.classList.add('fa-toggle-off'); - - pre_block.querySelector('.language-rust').style.display = 'block'; - pre_block.parentElement.nextElementSibling.style.display = 'none'; - - butt.style.right = "0px" - } else if (e.target.classList.contains('fa-toggle-off')) { - e.target.classList.remove('fa-toggle-off'); - e.target.classList.add('fa-toggle-on'); - - pre_block.querySelector('.language-rust').style.display = 'none'; - pre_block.parentElement.nextElementSibling.style.display = 'flex'; - - // resize code block only once - if (resize_done == false) { - sizeToFit(pre_block.parentElement.nextElementSibling.firstElementChild); - resize_done = true; - resized_butt_right = adjust_visualization_size(pre_block.parentElement.nextElementSibling); - } else { - butt.style.right = resized_butt_right + "px" - } - } - }); - } - }); - - if (window.playpen_copyable) { - Array.from(document.querySelectorAll('pre code')).forEach(function (block) { - var pre_block = block.parentNode; - if (!pre_block.classList.contains('playpen')) { - var buttons = pre_block.querySelector(".buttons"); - if (!buttons) { - buttons = document.createElement('div'); - buttons.className = 'buttons'; - pre_block.insertBefore(buttons, pre_block.firstChild); - } - - var clipButton = document.createElement('button'); - clipButton.className = 'fa fa-copy clip-button'; - clipButton.title = 'Copy to clipboard'; - clipButton.setAttribute('aria-label', clipButton.title); - clipButton.innerHTML = ''; - - buttons.insertBefore(clipButton, buttons.firstChild); - } - }); - } - - // Process playpen code blocks - Array.from(document.querySelectorAll(".playpen")).forEach(function (pre_block) { - // Add play button - var buttons = pre_block.querySelector(".buttons"); - if (!buttons) { - buttons = document.createElement('div'); - buttons.className = 'buttons'; - pre_block.insertBefore(buttons, pre_block.firstChild); - } - - var runCodeButton = document.createElement('button'); - runCodeButton.className = 'fa fa-play play-button'; - runCodeButton.hidden = true; - runCodeButton.title = 'Run this code'; - runCodeButton.setAttribute('aria-label', runCodeButton.title); - - buttons.insertBefore(runCodeButton, buttons.firstChild); - runCodeButton.addEventListener('click', function (e) { - run_rust_code(pre_block); - }); - - if (window.playpen_copyable) { - var copyCodeClipboardButton = document.createElement('button'); - copyCodeClipboardButton.className = 'fa fa-copy clip-button'; - copyCodeClipboardButton.innerHTML = ''; - copyCodeClipboardButton.title = 'Copy to clipboard'; - copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title); - - buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild); - } - - let code_block = pre_block.querySelector("code"); - if (window.ace && code_block.classList.contains("editable")) { - var undoChangesButton = document.createElement('button'); - undoChangesButton.className = 'fa fa-history reset-button'; - undoChangesButton.title = 'Undo changes'; - undoChangesButton.setAttribute('aria-label', undoChangesButton.title); - - buttons.insertBefore(undoChangesButton, buttons.firstChild); - - undoChangesButton.addEventListener('click', function () { - let editor = window.ace.edit(code_block); - editor.setValue(editor.originalCode); - editor.clearSelection(); - }); - } - }); -})(); - -(function themes() { - var html = document.querySelector('html'); - var themeToggleButton = document.getElementById('theme-toggle'); - var themePopup = document.getElementById('theme-list'); - var themeColorMetaTag = document.querySelector('meta[name="theme-color"]'); - var stylesheets = { - ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"), - tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"), - highlight: document.querySelector("[href$='highlight.css']"), - }; - - function showThemes() { - themePopup.style.display = 'block'; - themeToggleButton.setAttribute('aria-expanded', true); - themePopup.querySelector("button#" + document.body.className).focus(); - } - - function hideThemes() { - themePopup.style.display = 'none'; - themeToggleButton.setAttribute('aria-expanded', false); - themeToggleButton.focus(); - } - - function set_theme(theme, store = true) { - let ace_theme; - - if (theme == 'coal' || theme == 'navy') { - stylesheets.ayuHighlight.disabled = true; - stylesheets.tomorrowNight.disabled = false; - stylesheets.highlight.disabled = true; - - ace_theme = "ace/theme/tomorrow_night"; - } else if (theme == 'ayu') { - stylesheets.ayuHighlight.disabled = false; - stylesheets.tomorrowNight.disabled = true; - stylesheets.highlight.disabled = true; - ace_theme = "ace/theme/tomorrow_night"; - } else { - stylesheets.ayuHighlight.disabled = true; - stylesheets.tomorrowNight.disabled = true; - stylesheets.highlight.disabled = false; - ace_theme = "ace/theme/dawn"; - } - - setTimeout(function () { - themeColorMetaTag.content = getComputedStyle(document.body).backgroundColor; - }, 1); - - if (window.ace && window.editors) { - window.editors.forEach(function (editor) { - editor.setTheme(ace_theme); - }); - } - - var previousTheme; - try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { } - if (previousTheme === null || previousTheme === undefined) { previousTheme = default_theme; } - - if (store) { - try { localStorage.setItem('mdbook-theme', theme); } catch (e) { } - } - - html.classList.remove(previousTheme); - html.classList.add(theme); - } - - // Set theme - var theme; - try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } - if (theme === null || theme === undefined) { theme = default_theme; } - - set_theme(theme, false); - - themeToggleButton.addEventListener('click', function () { - if (themePopup.style.display === 'block') { - hideThemes(); - } else { - showThemes(); - } - }); - - themePopup.addEventListener('click', function (e) { - var theme = e.target.id || e.target.parentElement.id; - set_theme(theme); - }); - - themePopup.addEventListener('focusout', function(e) { - // e.relatedTarget is null in Safari and Firefox on macOS (see workaround below) - if (!!e.relatedTarget && !themeToggleButton.contains(e.relatedTarget) && !themePopup.contains(e.relatedTarget)) { - hideThemes(); - } - }); - - // Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628 - document.addEventListener('click', function(e) { - if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) { - hideThemes(); - } - }); - - document.addEventListener('keydown', function (e) { - if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; } - if (!themePopup.contains(e.target)) { return; } - - switch (e.key) { - case 'Escape': - e.preventDefault(); - hideThemes(); - break; - case 'ArrowUp': - e.preventDefault(); - var li = document.activeElement.parentElement; - if (li && li.previousElementSibling) { - li.previousElementSibling.querySelector('button').focus(); - } - break; - case 'ArrowDown': - e.preventDefault(); - var li = document.activeElement.parentElement; - if (li && li.nextElementSibling) { - li.nextElementSibling.querySelector('button').focus(); - } - break; - case 'Home': - e.preventDefault(); - themePopup.querySelector('li:first-child button').focus(); - break; - case 'End': - e.preventDefault(); - themePopup.querySelector('li:last-child button').focus(); - break; - } - }); -})(); - -(function sidebar() { - var html = document.querySelector("html"); - var sidebar = document.getElementById("sidebar"); - var sidebarScrollBox = document.querySelector(".sidebar-scrollbox"); - var sidebarLinks = document.querySelectorAll('#sidebar a'); - var sidebarToggleButton = document.getElementById("sidebar-toggle"); - var sidebarResizeHandle = document.getElementById("sidebar-resize-handle"); - var firstContact = null; - - function showSidebar() { - html.classList.remove('sidebar-hidden') - html.classList.add('sidebar-visible'); - Array.from(sidebarLinks).forEach(function (link) { - link.setAttribute('tabIndex', 0); - }); - sidebarToggleButton.setAttribute('aria-expanded', true); - sidebar.setAttribute('aria-hidden', false); - try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { } - } - - - var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle'); - - function toggleSection(ev) { - ev.currentTarget.parentElement.classList.toggle('expanded'); - } - - Array.from(sidebarAnchorToggles).forEach(function (el) { - el.addEventListener('click', toggleSection); - }); - - function hideSidebar() { - html.classList.remove('sidebar-visible') - html.classList.add('sidebar-hidden'); - Array.from(sidebarLinks).forEach(function (link) { - link.setAttribute('tabIndex', -1); - }); - sidebarToggleButton.setAttribute('aria-expanded', false); - sidebar.setAttribute('aria-hidden', true); - try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { } - } - - // Toggle sidebar - sidebarToggleButton.addEventListener('click', function sidebarToggle() { - if (html.classList.contains("sidebar-hidden")) { - showSidebar(); - } else if (html.classList.contains("sidebar-visible")) { - hideSidebar(); - } else { - if (getComputedStyle(sidebar)['transform'] === 'none') { - hideSidebar(); - } else { - showSidebar(); - } - } - }); - - sidebarResizeHandle.addEventListener('mousedown', initResize, false); - - function initResize(e) { - window.addEventListener('mousemove', resize, false); - window.addEventListener('mouseup', stopResize, false); - html.classList.add('sidebar-resizing'); - } - function resize(e) { - document.documentElement.style.setProperty('--sidebar-width', (e.clientX - sidebar.offsetLeft) + 'px'); - } - //on mouseup remove windows functions mousemove & mouseup - function stopResize(e) { - html.classList.remove('sidebar-resizing'); - window.removeEventListener('mousemove', resize, false); - window.removeEventListener('mouseup', stopResize, false); - } - - document.addEventListener('touchstart', function (e) { - firstContact = { - x: e.touches[0].clientX, - time: Date.now() - }; - }, { passive: true }); - - document.addEventListener('touchmove', function (e) { - if (!firstContact) - return; - - var curX = e.touches[0].clientX; - var xDiff = curX - firstContact.x, - tDiff = Date.now() - firstContact.time; - - if (tDiff < 250 && Math.abs(xDiff) >= 150) { - if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300)) - showSidebar(); - else if (xDiff < 0 && curX < 300) - hideSidebar(); - - firstContact = null; - } - }, { passive: true }); - - // Scroll sidebar to current active section - var activeSection = document.getElementById("sidebar").querySelector(".active"); - if (activeSection) { - sidebarScrollBox.scrollTop = activeSection.offsetTop; - } -})(); - -(function chapterNavigation() { - document.addEventListener('keydown', function (e) { - if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; } - if (window.search && window.search.hasFocus()) { return; } - - switch (e.key) { - case 'ArrowRight': - e.preventDefault(); - var nextButton = document.querySelector('.nav-chapters.next'); - if (nextButton) { - window.location.href = nextButton.href; - } - break; - case 'ArrowLeft': - e.preventDefault(); - var previousButton = document.querySelector('.nav-chapters.previous'); - if (previousButton) { - window.location.href = previousButton.href; - } - break; - } - }); -})(); - -(function clipboard() { - var clipButtons = document.querySelectorAll('.clip-button'); - - function hideTooltip(elem) { - elem.firstChild.innerText = ""; - elem.className = 'fa fa-copy clip-button'; - } - - function showTooltip(elem, msg) { - elem.firstChild.innerText = msg; - elem.className = 'fa fa-copy tooltipped'; - } - - var clipboardSnippets = new ClipboardJS('.clip-button', { - text: function (trigger) { - hideTooltip(trigger); - let playpen = trigger.closest("pre"); - return playpen_text(playpen); - } - }); - - Array.from(clipButtons).forEach(function (clipButton) { - clipButton.addEventListener('mouseout', function (e) { - hideTooltip(e.currentTarget); - }); - }); - - clipboardSnippets.on('success', function (e) { - e.clearSelection(); - showTooltip(e.trigger, "Copied!"); - }); - - clipboardSnippets.on('error', function (e) { - showTooltip(e.trigger, "Clipboard error!"); - }); -})(); - -(function scrollToTop () { - var menuTitle = document.querySelector('.menu-title'); - - menuTitle.addEventListener('click', function () { - document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' }); - }); -})(); - -(function controllMenu() { - var menu = document.getElementById('menu-bar'); - - (function controllPosition() { - var scrollTop = document.scrollingElement.scrollTop; - var prevScrollTop = scrollTop; - var minMenuY = -menu.clientHeight - 50; - // When the script loads, the page can be at any scroll (e.g. if you reforesh it). - menu.style.top = scrollTop + 'px'; - // Same as parseInt(menu.style.top.slice(0, -2), but faster - var topCache = menu.style.top.slice(0, -2); - menu.classList.remove('sticky'); - var stickyCache = false; // Same as menu.classList.contains('sticky'), but faster - document.addEventListener('scroll', function () { - scrollTop = Math.max(document.scrollingElement.scrollTop, 0); - // `null` means that it doesn't need to be updated - var nextSticky = null; - var nextTop = null; - var scrollDown = scrollTop > prevScrollTop; - var menuPosAbsoluteY = topCache - scrollTop; - if (scrollDown) { - nextSticky = false; - if (menuPosAbsoluteY > 0) { - nextTop = prevScrollTop; - } - } else { - if (menuPosAbsoluteY > 0) { - nextSticky = true; - } else if (menuPosAbsoluteY < minMenuY) { - nextTop = prevScrollTop + minMenuY; - } - } - if (nextSticky === true && stickyCache === false) { - menu.classList.add('sticky'); - stickyCache = true; - } else if (nextSticky === false && stickyCache === true) { - menu.classList.remove('sticky'); - stickyCache = false; - } - if (nextTop !== null) { - menu.style.top = nextTop + 'px'; - topCache = nextTop; - } - prevScrollTop = scrollTop; - }, { passive: true }); - })(); - (function controllBorder() { - menu.classList.remove('bordered'); - document.addEventListener('scroll', function () { - if (menu.offsetTop === 0) { - menu.classList.remove('bordered'); - } else { - menu.classList.add('bordered'); - } - }, { passive: true }); - })(); -})(); diff --git a/visualization.css b/visualization.css deleted file mode 100644 index 6fc0db9..0000000 --- a/visualization.css +++ /dev/null @@ -1,16 +0,0 @@ -/* flex related styling */ -.flex-container { - display: flex; - flex-direction: row; - justify-content: flex-start; - flex-wrap: nowrap; - flex-shrink: 0; -} - -object.tl_panel { - flex-grow: 0; -} - -object.code_panel { - flex-grow: 1; -} \ No newline at end of file