diff --git a/design-principles/TU_Signet.jpg b/design-principles/TU_Signet.jpg new file mode 100644 index 0000000000000000000000000000000000000000..c0d3df92f4235fe5d94ffc554c3bf4d52e592c94 Binary files /dev/null and b/design-principles/TU_Signet.jpg differ diff --git a/design-principles/beamerthemetuw.sty b/design-principles/beamerthemetuw.sty new file mode 100644 index 0000000000000000000000000000000000000000..ec54988ea0d11ee2e071e54a50a966a7dbebddb7 --- /dev/null +++ b/design-principles/beamerthemetuw.sty @@ -0,0 +1,315 @@ +%% +% @file beamerthemeTUW.sty +% @date 2016-08-30 +% @author Wolfgang Puffitsch, Denise Ratasich +% +% @brief Beamer LaTeX template matching the corporate design of TU Wien. +%% + +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{beamerthemeTUW}[2016/08/30 Beamer template that fits the TU Wien corporate design (adopted styles from powerpoint presentation template in CD manual)] + +\RequirePackage{graphicx} +\RequirePackage{tikz} + +\def\beamer@tuwDefault{default} +\def\beamer@tuwPlain{plain} +\def\beamer@tuwSidebar{sidebar} +\def\beamer@tuwHelvetica{helvetica} +\def\beamer@tuwNoImage{none} +\def\beamer@tuwLogo{tuw-logo} +\def\beamer@tuwLogoWhite{TU_Logo_white} +\def\beamer@tuwSecondLogo{none} +\def\beamer@tuwSecondLogoWidth{.5\textwidth} + +%%% options +% The user may choose other fonts (TU Wien CD says to use helvetica). +\DeclareOptionBeamer{font}{\def\beamer@tuwFont{#1}} +% Another outer theme may be chosen, however it may not fit the TU Wien colors. +\DeclareOptionBeamer{outer}{\def\beamer@tuwOuter{#1}} +% Width of the sidebar. +\DeclareOptionBeamer{width}{\def\beamer@tuwWidth{#1}} +% Background image of TU Wien building on title page. +\newif\ifbeamer@tuwBackground +\DeclareOptionBeamer{tuw_background}{\beamer@tuwBackgroundtrue} +% Individual background image on title page. +\DeclareOptionBeamer{tuw_image}{\def\beamer@tuwImage{#1}} +% White logo (if tuw_image is dark the logo should be set to be white). +\newif\ifbeamer@tuwWhiteLogo +\DeclareOptionBeamer{tuw_whitelogo}{\beamer@tuwWhiteLogotrue} +% Print frame title beside the logo to save space. +\newif\ifbeamer@tuwFrametitleToTop +\DeclareOptionBeamer{tuw_frametitletotop}{\beamer@tuwFrametitleToToptrue} +% Second logo in case we need more than one +\DeclareOptionBeamer{second_logo}{\def\beamer@tuwSecondLogo{#1}} +\DeclareOptionBeamer{second_logo_width}{\def\beamer@tuwSecondLogoWidth{#1}} + +% defaults +\ExecuteOptionsBeamer{outer=default} +\ExecuteOptionsBeamer{width=1.6cm} +\ExecuteOptionsBeamer{font=\familydefault} +\beamer@tuwBackgroundfalse +\ExecuteOptionsBeamer{tuw_image=none} +\ExecuteOptionsBeamer{second_logo=none} +\beamer@tuwWhiteLogofalse +\beamer@tuwFrametitleToTopfalse + +\ProcessOptionsBeamer + +\ifx\beamer@tuwFont\beamer@tuwHelvetica +\RequirePackage{helvet} +\fi + +\mode<presentation> + +%%% colors +% For some reason the logo is not in TU-blue (0,102,153). The powerpoint +% presentation has TU-blue as background and the logo with a different color +% (doesn't look nice). We align the TU-blue to the logo, like in the CD manual. +\definecolor{tuwBlue}{RGB}{0,116,178} +\definecolor{tuwGray}{RGB}{102,102,102} +\definecolor{iceBlue}{RGB}{222,231,236} + +\def\tuwBlue{tuwBlue} +\def\tuwGray{tuwGray} +\def\iceBlue{iceBlue} +\def\tuwLightBlue{tuwBlue!20!white} +\def\tuwMediumBlue{tuwBlue!45!white} +\def\tuwDarkBlue{tuwBlue!70!black} +\def\alertred{red!50!black} + +% content page +\setbeamercolor{normal text}{fg=black,bg=white} +\setbeamercolor{alerted text}{fg=\alertred} +\setbeamercolor{structure}{fg=\tuwBlue} +\setbeamercolor{frametitle}{fg=black} + +\setbeamercolor{palette primary}{fg=\tuwDarkBlue,bg=\tuwLightBlue} +\setbeamercolor{palette secondary}{fg=\tuwBlue!50!black,bg=\tuwMediumBlue} +\setbeamercolor{palette tertiary}{fg=white,bg=\tuwBlue} + +\setbeamercolor{section in toc}{parent=normal text} +\setbeamercolor{subsection in toc}{parent=section in toc} + +\setbeamercolor{block title}{fg=white, bg=\tuwBlue} +\setbeamercolor{block title example}{fg=white, bg=\tuwBlue} +\setbeamercolor{block title alerted}{fg=white, bg=\alertred} +\setbeamercolor{block body}{bg=\iceBlue} +\setbeamercolor{block body example}{parent=block body} +\setbeamercolor{block body alerted}{parent=block body, fg=\alertred} + +\setbeamercolor{title in head/foot}{fg=\tuwBlue} +\setbeamercolor{page number in head/foot}{fg=\tuwBlue} +\setbeamercolor{footline}{fg=\tuwBlue} + +% color difference of default and other outer styles +\ifx\beamer@tuwOuter\beamer@tuwDefault +% default style - this is the CD of TU Wien +% title page +\setbeamercolor{title}{fg=white} +\setbeamercolor{subtitle}{fg=white} +\setbeamercolor{author}{fg=white} +\setbeamercolor{institute}{fg=white} +\setbeamercolor{date}{fg=white} +% content page +% keep defaults (black) +\else +% other outer styles (e.g., sidebar) +% title page +\setbeamercolor{title}{fg=\tuwGray} +\setbeamercolor{subtitle}{fg=\tuwGray} +\setbeamercolor{author}{fg=black} +\setbeamercolor{institute}{fg=black} +\setbeamercolor{date}{fg=black} +% content page +\setbeamercolor{frametitle}{fg=\tuwGray} +\setbeamercolor{headline}{fg=\tuwGray} +\setbeamercolor{footline}{fg=\tuwGray} +\fi + +% sidebar +\setbeamercolor{sidebar}{bg=\tuwBlue,fg=white} +\setbeamercolor{palette sidebar primary}{fg=\tuwDarkBlue,bg=\tuwLightBlue} +\setbeamercolor{palette sidebar secondary}{fg=\tuwDarkBlue,bg=\tuwLightBlue} +\setbeamercolor{palette sidebar tertiary}{fg=white,bg=\tuwBlue} +\setbeamercolor{title in sidebar}{fg=white,bg=\tuwBlue} +\setbeamercolor{section in sidebar}{fg=\tuwDarkBlue} +\setbeamercolor{section in sidebar shaded}{fg=white,bg=\tuwBlue} +\setbeamercolor{subsection in sidebar}{fg=\tuwDarkBlue} +\setbeamercolor{subsection in sidebar shaded}{fg=white,bg=\tuwBlue} + +%%% fonts + +% font weight +\setbeamerfont{title}{series=\bfseries} +\setbeamerfont{author}{series=\mdseries} +\setbeamerfont{institute}{series=\mdseries} +\setbeamerfont{date}{series=\mdseries} +\setbeamerfont{frametitle}{series=\mdseries} +\setbeamerfont{headline}{series=\mdseries} +\setbeamerfont{footline}{series=\mdseries} + +% font size difference of default and other outer styles +\ifx\beamer@tuwOuter\beamer@tuwDefault +% default style - this is the CD of TU Wien +\setbeamerfont{title}{size=\huge} +\setbeamerfont{author}{size=\normalsize} +\setbeamerfont{institute}{size=\normalsize} +\setbeamerfont{date}{size=\normalsize} +\setbeamerfont{frametitle}{size=\Large} +\setbeamerfont{framesubtitle}{size=\large} +\setbeamerfont{headline}{size=\scriptsize} +\setbeamerfont{footline}{size=\scriptsize} +\else +% other outer styles (e.g., sidebar) +% keep defaults +\fi + +\setbeamerfont{section in toc}{parent=normal text} +\setbeamerfont{subsection in toc}{parent=section in toc} + +% sidebar +\setbeamerfont{title in sidebar}{series=\bfseries} +\setbeamerfont{section in sidebar}{series=\bfseries,size=\tiny} +\setbeamerfont{subsection in sidebar}{size=\tiny} + +%%% outer +\setbeamersize{text margin left=1.2cm} +\setbeamersize{text margin right=1.5cm} +\beamertemplatenavigationsymbolsempty + + +\ifx\beamer@tuwOuter\beamer@tuwDefault +% default outher theme - this is the CD of TU Wien + +% background +\setbeamertemplate{background}{ + \begin{tikzpicture} + \useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight); + % title page + \ifbeamer@tuwBackground % tu building background selected + \node[anchor=south west] (background) at (-0.15cm,-0.15cm) {\includegraphics[width=1.01\paperwidth]{TU_Background}}; + \else\ifx\beamer@tuwImage\beamer@tuwNoImage % no background image specified + \fill[white] (0,0) rectangle (\the\paperwidth,\the\paperheight); + \else % individual background image + \node[anchor=north west] (background) at (-0.15cm,-0.15cm) {\includegraphics[width=1.01\paperwidth]{\beamer@tuwImage}}; + \fi\fi + \fill[\tuwBlue, rounded corners=0.5cm] (-1cm,-1cm) rectangle (\paperwidth-0.5cm,\paperheight-3cm); + \ifbeamer@tuwWhiteLogo % white logo option specified + \node[anchor=north west] (logo) at (0.3cm,\paperheight-0.1cm) {\includegraphics[width=.5\textwidth]{\beamer@tuwLogoWhite}}; + \else + \node[anchor=north west] (logo) at (0.2cm,\paperheight-0.2cm) {\includegraphics[height=2cm]{\beamer@tuwLogo}}; + \fi + \ifx\beamer@tuwSecondLogo\beamer@tuwNoImage + \else + \node[anchor=north east] (logo2) at (\paperwidth-0.2cm,\paperheight-0.2cm) {\includegraphics[width=\beamer@tuwSecondLogoWidth]{\beamer@tuwSecondLogo}}; + \fi + % content page + \ifnum\thepage>1\relax% + \fill[\iceBlue] (0,0) rectangle (\the\paperwidth,\the\paperheight); + \fill[white, rounded corners=0.5cm] (-1cm,-1cm) rectangle(\paperwidth-0.4cm,\paperheight-0.8cm); + \node[anchor=west] (logo) at (0.3cm,\paperheight-0.4cm) {\includegraphics[height=0.5cm]{TU_Signet}}; + \fi + \end{tikzpicture} +} + +% title page +\def\beamer@titlepageLeft{1.6cm} +\setbeamertemplate{title page}{ + \vskip 4cm% + \begin{beamercolorbox}[leftskip=\beamer@titlepageLeft]{title}% + \usebeamerfont{title}\inserttitle% + \end{beamercolorbox}% + \vskip 0.5cm% + \begin{beamercolorbox}[leftskip=\beamer@titlepageLeft]{subtitle} + \usebeamerfont{subtitle}\insertsubtitle% + \end{beamercolorbox} + \vskip 0.25cm% + \vfill% + \begin{beamercolorbox}[leftskip=\beamer@titlepageLeft]{author} + \usebeamerfont{author}\insertauthor% + \end{beamercolorbox} + \begin{beamercolorbox}[leftskip=\beamer@titlepageLeft]{date} + \usebeamerfont{date}\insertdate% + \end{beamercolorbox} +} + +% frametitle +\setbeamertemplate{frametitle} +{% + \ifbeamer@tuwFrametitleToTop % frametitle should be printed beside the logo + % vbox to keep the baseline + \vbox{}\vskip -0.43cm% + \else + \vbox{}\vskip 0.7cm% + \fi + \begin{beamercolorbox}[leftskip=0cm]{frametitle} + \usebeamerfont{frametitle}\insertframetitle% + \ifbeamer@tuwFrametitleToTop + \vbox{}\vskip 0.3cm% + \else + \vbox{}\vskip 0.2cm% + \fi + \usebeamerfont{framesubtitle}\insertframesubtitle% + \end{beamercolorbox} +} + +% footline +\setbeamertemplate{footline}{% + \vspace{0.1cm} + \ifnum\thepage>1 + \begin{beamercolorbox}[wd=\paperwidth,ht=2.25ex,dp=1ex]{footline} + \hskip 1.2cm% + \usebeamerfont{footline}\insertshortdate% + \hfill% + \insertshorttitle, \insertshortauthor% + \hfill% + \insertframenumber\:/\:\inserttotalframenumber% + \hspace*{1.5cm}% + \end{beamercolorbox} + \else + \begin{beamercolorbox}[wd=\paperwidth,ht=2.25ex,dp=1ex,center]{}% + % empty environment to raise height + \end{beamercolorbox} + \fi + \vspace{0.1cm} +} + +\else\ifx\beamer@tuwOuter\beamer@tuwSidebar +% sidebar outer theme +\useoutertheme[width=\beamer@tuwWidth,height=0pt]{\beamer@tuwOuter} + +% footline +\setbeamertemplate{footline}{ + \ifnum\thepage>1 + \vspace{-1cm} + \hfill{\scriptsize\insertframenumber\:/\:\inserttotalframenumber~~} + \vspace{0.1cm} + \fi +} +\else +\useoutertheme{\beamer@tuwOuter} +\fi\fi % if outer=default or outer=sidebar + + +%%% inner +\ifx\beamer@tuwOuter\beamer@tuwDefault +\setbeamertemplate{itemize items}[circle] +\else +% defaults +\fi % if outer=default + +\setlength{\parskip}{6pt} + +% blocks +\setbeamertemplate{blocks}[rounded] +\pgfdeclareverticalshading{bmb@transition}{\the\paperwidth}{color(0pt)=(\iceBlue); color(3.0pt)=(\iceBlue)} + +\setbeamercovered{invisible} + +% emphasize +\renewcommand<>{\emph}[1]{{\only#2{\color{\tuwBlue}}#1}} + +\hypersetup{colorlinks=true,linkcolor=fg,citecolor=\tuwGray,urlcolor=\tuwGray} + +\mode<all> diff --git a/design-principles/presentation.tex b/design-principles/presentation.tex new file mode 100644 index 0000000000000000000000000000000000000000..05a6d9788859f9f56577d3c8ff2017b41a021875 --- /dev/null +++ b/design-principles/presentation.tex @@ -0,0 +1,139 @@ + +\documentclass[10pt]{beamer} + +\usetheme[font=helvetica]{TUW} + +\title{Rust Design Principles} +\author{Florian Sextl \and Mark Chimes \and Adrian Rebola-Pardo} +\date{2025-03-25} + +\begin{document} + +\begin{frame} +\titlepage +\end{frame} + +\begin{frame}{Rust in a nutshell} + Rust is a systems programming language. + \begin{itemize} + \item fine-grained control of memory and resources + \item high-level abstractions to do so + \item abstractions with different cost structures (including zero-cost) + \end{itemize} + + Yes, Rust has pointers. But you will \textit{actively avoid} using them.\\ + {\small\url{https://doc.rust-lang.org/std/primitive.pointer.html\#method.add-1}} + + Custom allocation is \textit{still} experimental!\\ + {\small\url{https://doc.rust-lang.org/std/alloc/trait.Allocator.html}} +\end{frame} + +\begin{frame}{The goals of Rust} + At this point a meme: \emph{blazingly fast} + + Full resource control behind locked-down interfaces. + + Highly long-term maintainable code, at the expense of short-term development time. + + Well-designed APIs do not need the developer to mind the gap; + \emph{memory safety} as the ultimate gap. + + Extensive \textit{and} extensible ecosystem (Cargo, rustdoc). +\end{frame} + +\begin{frame}{Buzzwords for Rust} +Buzzwords that apply to Rust +\begin{itemize} + \item \emph{Procedural}: explicit control flow + \item \emph{Statically typed}: with generics but not dependent types + \item \emph{Concurrent}: async in the type system, futures/atomics through std + \item \emph{RAII}: low-key the cornerstone of Rust +\end{itemize} + +Buzzwords that only sort of apply to Rust +\begin{itemize} + \item \emph{Declarative}: only for type bounds (but strongly so) + \item \emph{Functional}: strong use of closures, unique function typing, + function ``types'' as interfaces + \item \emph{Metaprogramming}: generics implemented through templates, + but modeled through constraint problems +\end{itemize} +\end{frame} + +\begin{frame}{Rust is data-oriented} +Rust follows the RAII paradigm: +\begin{itemize} + \item No constructors: any function returning a value is responsible for resource allocation + \item Visibility control enforces correct resource allocation + \item Everything is an ``object'': privileged traits control byte-clone correctness (Copy) + and resource deallocation (Drop). + \item Types have associated methods. + \item Traits (interfaces) control specialization. +\end{itemize} + +In Rust there are no ``objects'', there are only values. +\end{frame} + +\begin{frame}{Rust is not object-oriented} +Rust is not OO, with full intent: +\begin{itemize} + \item Associated methods are just functions. + \item No inheritance, no subclassing: interface relations expressed through bounds. + \item The diamond problem is a compile error. + \item Dynamic dispatch imposes additional restrictions on traits. +\end{itemize} + +OO: types are data and the methods that operate on it. + +Rust: types are \emph{control over} data and \emph{invariants} over the methods that operate on it. +\end{frame} + +\begin{frame}{Ownership} + A value \emph{recursively owns} resources: those resources can only be accessed by borrowing from the value. + \begin{itemize} + \item \emph{Shared borrowing}: several immutable borrows are simultaneously possible + \item \emph{Mutable borrowing}: a unique mutable borrow is simultaneously possible + \item \emph{The golden rule of Rust}: shared XOR mutable (else a compile-time error) + \item \emph{Not so obvious conclusion}: borrowing \textit{is a resource}. + \end{itemize} +\end{frame} + +\begin{frame}{Spicy pass-by-value} + Rust values free their allocated resources when they go out of scope. + + Rust arguments are \textit{always} pass-by-value (so they go out of scope at return). + + References are values that own borrows for a given \emph{lifetime}: + \begin{itemize} + \item \texttt{\&'a T}: immutable borrow of a \texttt{T} value for lifetime \texttt{'a} + \item \texttt{\&'a mut T}: mutable borrow of a \texttt{T} value for lifetime \texttt{'a} + \end{itemize} + + \emph{Your task as a Rust programmer}: convincing the borrow checker that the way you are accessing + data does indeed agree with \emph{shared XOR mutable}. +\end{frame} + +\begin{frame}{Safe vs unsafe Rust} +\emph{Your task as a Rust programmer}: convincing the borrow checker that the way you are accessing +data does indeed agree with \emph{shared XOR mutable} + +The borrow checker is overprotective: sometimes what you want to do is \emph{sound} (respects the borrow rules) +but \emph{unsafe} (cannot be guaranteed by the compiler) + +\emph{An escape hatch}: \texttt{unsafe} blocks allow the developer to avoid compiler oversight +(but \emph{absolutely not} the borrow rules). +\end{frame} + +\begin{frame}{Zero-cost abstractions} +Rust encourages composition to enforce invariants. + +\emph{Typical case}: your type is a tower of wrappers, each enforcing a stronger invariant + +\emph{Zero-cost abstraction} +\begin{itemize} +\item Memory layout and internal representation are (mostly) handled by the compiler +\item Simple call-passing is often compiled away +\item Simple generics and trait-based implementations often compile away into mere control flow (e.g. iterators) +\end{itemize} +\end{frame} +\end{document} \ No newline at end of file diff --git a/design-principles/tuw-logo.pdf b/design-principles/tuw-logo.pdf new file mode 100644 index 0000000000000000000000000000000000000000..77796289c36ff06396de70cc496fd0edbb597a9b Binary files /dev/null and b/design-principles/tuw-logo.pdf differ diff --git a/no-unsafe-no-bugs/Cargo.toml b/no-unsafe-no-bugs/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..db74d9ec7b89c0503ea7dbdd9be1f9de1b3f0152 --- /dev/null +++ b/no-unsafe-no-bugs/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "no-unsafe-no-bugs" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/no-unsafe-no-bugs/src/main.rs b/no-unsafe-no-bugs/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..776257b1dcbdabb3badf623bad42373560462042 --- /dev/null +++ b/no-unsafe-no-bugs/src/main.rs @@ -0,0 +1,21 @@ +use std::cell::RefCell; + +fn main() { + + let vec = Vec::<i32>::new(); + + // `RefCell<T>` allows runtime-checked mutable access to a `T`. + let cell = RefCell::new(vec); + + // I can take a mutable borrow. + let mut borrow1 = cell.borrow_mut(); + + // But I cannot take two! + let mut borrow2 = cell.borrow_mut(); + + // If you don't write unsafe, you won't UB... + // so instead you crash and burn. + + borrow1.push(1); + borrow2.push(2); +} diff --git a/safe-means-safe/Cargo.toml b/safe-means-safe/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..9a71f864433cf3e91f51736fb583cbc97e549c27 --- /dev/null +++ b/safe-means-safe/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "safe-means-safe" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/safe-means-safe/src/main.rs b/safe-means-safe/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..c77f65300af2cf81145f4732db59ef480b91ee23 --- /dev/null +++ b/safe-means-safe/src/main.rs @@ -0,0 +1,29 @@ +#[forbid(unsafe_code)] +fn main() { + // Let's swap two items in a `Vec`. + let mut vec = Vec::from([1985, 2025]); + + // First try: grab mutable references to the items, then swap them. + let first = vec.get_mut(0).unwrap(); + let second = vec.get_mut(1).unwrap(); + std::mem::swap(first, second); + + // // Second try: use a standard library function + // vec.swap(0, 1); + + // Much safe! Very concurrency! + println!("OUTPUT: {:?}", &vec); +} + +// Claim 1: Creating two mutable references to (different) places +// within the memory owned by the same object is only possible +// through unsafe Rust. +// +// Claim 2: #[forbid(unsafe_code)] guarantees that no unsafe +// code is ever used. +// +// Claim 3: I need two mutable references to places in the +// memory owned by a `Vec` in order to swap them. +// +// +// Pick two. \ No newline at end of file diff --git a/unsafe-rust-is-cpp/Cargo.toml b/unsafe-rust-is-cpp/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..84993520a967e98e538407a23ec35187b80d3319 --- /dev/null +++ b/unsafe-rust-is-cpp/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "unsafe-rust-is-cpp" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/unsafe-rust-is-cpp/src/main.rs b/unsafe-rust-is-cpp/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..3db7aac03945744bec203e76f6d6d887d02c9522 --- /dev/null +++ b/unsafe-rust-is-cpp/src/main.rs @@ -0,0 +1,49 @@ +// Example by Jack O'Connor +// https://www.youtube.com/watch?v=DG-VLezRkYQ + +pub fn main() { + rust_like_its_1985(); +} + +pub fn rust_like_its_1985() { + let mut n = 0; + + // Taking a raw pointer is safe! + let n_ptr = &mut n as *mut i32; + + // Dereferencing a pointer is unsafe, but not necessarily UB. + let n_mut1 = unsafe { &mut *n_ptr }; + + // UB: Multiple mutable references to the same place + let n_mut2 = unsafe { &mut *n_ptr }; + + // Or is it? I can just remember not to operate on them at the same time. + *n_mut1 = 42; + + // Right? + *n_mut2 = 0; + + // "It works for me" + println!("OUTPUT: {}", *n_mut1); +} + +pub fn rust_like_its_2025() { + let mut n = 0; + let n_ptr = &mut n as *mut i32; + let n_mut1 = unsafe { &mut *n_ptr }; + let n_mut2 = unsafe { &mut *n_ptr }; + + // So far this is the same. Let's move the rest to a function. + println!("OUTPUT: {}", foo(n_mut1, n_mut2)); + + // You can't code in unsafe Rust like it's C++. + // Unsafe Rust does not allow you to jump over the borrow rules. + // It merely stops preventing you from trying. +} + +// #[inline(never)] +pub fn foo(x: &mut i32, y: &mut i32) -> i32 { + *x = 42; + *y = 0; + *x +} \ No newline at end of file