\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Durobot} \pdfinfo{ /Title (v-language-types.pdf) /Creator (Cheatography) /Author (Durobot) /Subject (V language types Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{98B572} \definecolor{LightBackground}{HTML}{F8FAF6} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{V language types Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Durobot} via \textcolor{DarkBackground}{\uline{cheatography.com/148384/cs/32362/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Durobot \\ \uline{cheatography.com/durobot} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 29th June, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{p{1.04 cm} x{6.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Primitive types}} \tn % Row 0 \SetRowColor{LightBackground} bool & 8 bits, boolean \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \seqsplit{string} & An {\bf{immutable}} array of bytes. All Unicode characters are UTF-8.\{\{nl\}\}Indexing a string produces a byte, not a rune nor another string. Indexes correspond to bytes in the string, not Unicode code points.\{\{nl\}\}Use single or double quotes to denote strings. \tn % Row Count 9 (+ 8) % Row 2 \SetRowColor{LightBackground} rune & 32 bits, unicode code point (\textasciitilde{}character), alias for u32.\{\{nl\}\}To denote runes, use ` (backticks). \tn % Row Count 12 (+ 3) % Row 3 \SetRowColor{white} i8 & 8 bits, signed integer \tn % Row Count 13 (+ 1) % Row 4 \SetRowColor{LightBackground} i16 & 16 bits, signed integer \tn % Row Count 14 (+ 1) % Row 5 \SetRowColor{white} int & 32 bits, signed integer (the default int type) \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} i64 & 64 bits, signed integer \tn % Row Count 17 (+ 1) % Row 7 \SetRowColor{white} u8 & 8 bits, unsigned integer (alias for byte?) \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} u16 & 16 bits, unsigned integer \tn % Row Count 20 (+ 1) % Row 9 \SetRowColor{white} u32 & 32 bits, unsigned integer \tn % Row Count 21 (+ 1) % Row 10 \SetRowColor{LightBackground} u64 & 64 bits, signed integer \tn % Row Count 22 (+ 1) % Row 11 \SetRowColor{white} isize & System-dependent signed integer, 64 bits on x64 \tn % Row Count 24 (+ 2) % Row 12 \SetRowColor{LightBackground} usize & System-dependent unsigned integer, 64 bits on x64 \tn % Row Count 26 (+ 2) % Row 13 \SetRowColor{white} f32 & 32 bits, floating-point number \tn % Row Count 27 (+ 1) % Row 14 \SetRowColor{LightBackground} f64 & 64 bits, floating-point number (the default float type) \tn % Row Count 29 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Runes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Can be converted to a UTF-8 string by using the {\bf{.str()}} method:\{\{nl\}\}`rocket := \textbackslash{}`\{\{fa-rocket\}\} \textbackslash{}`\{\{nl\}\}assert rocket.str() == '\{\{fa-rocket\}\}'`} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Can be converted to UTF-8 bytes by using the {\bf{.bytes()}} method:\{\{nl\}\}`rkt := \textbackslash{}`\{\{fa-rocket\}\}\textbackslash{}`\{\{nl\}\}assert rkt.bytes() == {[}u8(0xf0),0x9f,0x9a,0x80{]}`} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Hex, Octal, and Unicode escape sequences work in rune literals:\{\{nl\}\}`assert \textbackslash{}`\textbackslash{}x61\textbackslash{}` == \textbackslash{}`a\textbackslash{}`\{\{nl\}\}assert \textbackslash{}`\textbackslash{}141\textbackslash{}` == \textbackslash{}`a\textbackslash{}`\{\{nl\}\}assert \textbackslash{}`\textbackslash{}u0061\textbackslash{}` == \textbackslash{}`a\textbackslash{}``} \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Multibyte literals work too:\{\{nl\}\}`assert \textbackslash{}`\textbackslash{}u2605\textbackslash{}` == \textbackslash{}`★\textbackslash{}`\{\{nl\}\}assert \textbackslash{}`\textbackslash{}u2605\textbackslash{}`.bytes() == {[}u8(0xe2), 0x98, 0x85{]}\{\{nl\}\}assert \textbackslash{}`\textbackslash{}xe2\textbackslash{}x98\textbackslash{}`.bytes() == {[}u8(0xe2),0x98{]}\{\{nl\}\}assert \textbackslash{}`\textbackslash{}342\textbackslash{}230\textbackslash{}`.bytes() == {[}u8(0xe2),0x98{]}`} \tn % Row Count 15 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{String interpolation}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Use {\bf{\$}} before a variable name. The variable will be converted to a string and embedded into the literal:\{\{nl\}\}`name := 'Bob'\{\{nl\}\}println('Hello, \$name!') // Hello, Bob!`\{\{nl\}\}Works with fields: `'age = \$user.age'`\{\{nl\}\}And with expressions: `'can register = \$\{user.age \textgreater{} 13\}'`} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Format specifier pattern}}\{\{nl\}\}`\$\{varname:{[}flags{]}{[}width{]}{[}.precision{]}{[}type{]}\}`\{\{nl\}\}* flags: {\bf{-}} to left-align output within the field, {\bf{+}} to right-align (default), {\bf{0}} (zero) to use `0` as the padding character instead of the default space\{\{nl\}\}* width: integer value - minimum width of total field to output\{\{nl\}\}* precision: an integer value preceded by a . Guarantees the number of digits after the decimal point, if the input is a float. Ignored if integer.\{\{nl\}\}* type: {\bf{f}} or {\bf{F}} - float, rendered as float; {\bf{e}} or {\bf{E}} - float, rendered as exponent; {\bf{g}} or {\bf{G}} - foat, small values rendered as float, large values as exponent; {\bf{d}} - integer, rendered as decimal; {\bf{x}} or {\bf{X}} - integer, rendered as hexadecimal; {\bf{o}} - integer, rendered as octal; {\bf{b}} - integer, rendered as binary; {\bf{s}} - string (almost never used).\{\{nl\}\}Format strings are parsed at compile time, specifying type can help detect errors.} \tn % Row Count 25 (+ 19) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`x := 123.4567\{\{nl\}\}'{[}\$\{int(x):010\}{]}' // left-pad with 0 =\textgreater{} {[}0000000123{]}\{\{nl\}\}'{[}\$\{10.0000:.2\}{]}' // strip insignificant 0s at the end =\textgreater{} {[}10{]}\{\{nl\}\} println('\$\{10.1234:.2\}') // =\textgreater{} 10.12\{\{nl\}\}println('{[}\$\{10.0000:.2f\}{]}') // show 0s at the end`} \tn % Row Count 30 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A collection of data elements of the same type:\{\{nl\}\}`mut nums := {[}1, 2, 3{]}\{\{nl\}\}println(nums{[}0{]}) // \textbackslash{}`1\textbackslash{}` - zero-indexed\{\{nl\}\}nums \textless{}\textless{} 4 // appending element, {[}1, 2, 3, 4{]}\{\{nl\}\}nums \textless{}\textless{} {[}5, 6{]} // appending array, {[}1, .., 5, 6{]}\{\{nl\}\}1 in nums // true\{\{nl\}\}x := nums{[}999{]} // panic, use {\bf{or \{ default value \}}}`} \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Arrays: Fields (read-only)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{len}}: length - the number of pre-allocated and initialized elements in the array;\{\{nl\}\}{\bf{cap}}: capacity - the amount of memory space which has been reserved for elements, but not initialized or counted as elements. The array can grow up to this size without being reallocated.\{\{nl\}\}`mut nums := {[}1, 2, 3{]}\{\{nl\}\}println(nums.len) // "3"\{\{nl\}\}println(nums.cap) // "3" or greater\{\{nl\}\}nums = {[}{]} // The array is now empty\{\{nl\}\}println(nums.len) // "0"`\{\{nl\}\}{\bf{data}} is a field (of type voidptr) with the address of the first element. This is for low-level unsafe code.} \tn % Row Count 12 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Arrays: Initialization}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Array type is determined by the first element:\{\{nl\}\}`{[}1, 2, 3{]}` is an array of ints (`{[}{]}int`)\{\{nl\}\}`{[}'a', 'b'{]}` is an array of strings (`{[}{]}string`)\{\{nl\}\}`{[}u8(16), 32, 64{]}` is an array of u8 (`{[}{]}u8`)\{\{nl\}\}Second initialization syntax:\{\{nl\}\}`mut a := {[}{]}int\{len: 10000, cap: 30000, init: 3\}`\{\{nl\}\}{\bf{len}} (default 0), {\bf{cap}} and {\bf{init}} (type default) are optional\{\{nl\}\}`users := {[}{]}int\{\} // empty array`\{\{nl\}\}`c := {[}{]}int\{len: 3, init: {\bf{it}}\} // {[}0, 1, 2{]} - it=index`} \tn % Row Count 10 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Arrays: Multidimensional}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`mut a := {[}{]}{[}{]}int\{len: 2, init: {[}{]}int\{len: 3\}\}\{\{nl\}\}a{[}0{]}{[}1{]} = 2`\{\{nl\}\}`mut a := {[}{]}{[}{]}{[}{]}int\{len: 2, init: {[}{]}{[}{]}int\{len: 3, init: {[}{]}int\{len: 2\}\}\}\{\{nl\}\}a{[}0{]}{[}1{]}{[}1{]} = 2`} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Fixed size arrays}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Arrays with fixed size (length). You cannot append elements to them, nor shrink them. You can only modify their elements in place.\{\{nl\}\}Access to the elements of fixed size arrays is more efficient, they need less memory than ordinary arrays, and unlike ordinary arrays, their data is on the stack, so you may want to use them as buffers if you do not want additional heap allocations.} \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Most methods are defined to work on ordinary arrays, not on fixed size arrays. You can convert a fixed size array to an ordinary array with slicing:\{\{nl\}\}`mut fnums := {[}3{]}int\{\} // fnums is a fixed size array with 3 elements.\{\{nl\}\}fnums{[}0{]} = 1\{\{nl\}\}fnums{[}1{]} = 10\{\{nl\}\}fnums{[}2{]} = 100\{\{nl\}\}println(fnums) // =\textgreater{} {[}1, 10, 100{]}\{\{nl\}\}println(typeof(fnums).name) // =\textgreater{} {[}3{]}int\{\{nl\}\}fnums2 := {[}1, 10, 100{]}! // short init syntax that does the same (the syntax will probably change)\{\{nl\}\}anums := fnums{[}..{]} // same as \textbackslash{}`anums := fnums{[}0..fnums.len{]}\textbackslash{}`\{\{nl\}\}println(anums) // =\textgreater{} {[}1, 10, 100{]}\{\{nl\}\}println(typeof(anums).name) // =\textgreater{} {[}{]}int`} \tn % Row Count 21 (+ 13) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Note that slicing will cause the {\bf{data}} of the fixed size array to be {\bf{copied to the newly created ordinary array}}.} \tn % Row Count 24 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Structs}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Structs are allocated on the stack.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`struct Point \{\{\{nl\}\}~~x int // immutable\{\{nl\}\}~~y int // immutable\{\{nl\}\}\}\{\{nl\}\}mut p := Point \{\{\{nl\}\}~~x: 10\{\{nl\}\}~~y: 20\{\{nl\}\}\}\{\{nl\}\}println(p.x) // Struct fields access\{\{nl\}\}p = Point\{10, 20\} // Alt syntax when \textless{}= 3 fields`} \tn % Row Count 7 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Struct fields are private (accessible within the module) and immutable by default. This can be changed with {\bf{pub}} and {\bf{mut}} modifiers. There are 5 options in total:\{\{nl\}\}`struct Foo \{\{\{nl\}\}~~a int // private immutable (default)\{\{nl\}\}mut:\{\{nl\}\}~~b int // private mutable\{\{nl\}\}~~c int // another one\{\{nl\}\}pub:\{\{nl\}\}~~d int // public immutable (readonly)\{\{nl\}\}pub mut:\{\{nl\}\}~~e int // public, mutable only in parent module\{\{nl\}\}\_\_global: // Not recommended\{\{nl\}\}~~f int // public and mutable everywhere\{\{nl\}\}\}`} \tn % Row Count 19 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Structs: Default field falues}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`struct Foo \{ // {\bf{fields are zeroed by default}}\{\{nl\}\}~~n int // n is 0 by default\{\{nl\}\}~~s string // s is '' by default\{\{nl\}\}~~a {[}{]}int // a is allocated (\textbackslash{}`{[}{]}int\{\}\textbackslash{}`) by default\{\{nl\}\}~~p int = -1 // custom default value\{\{nl\}\}~~r int {[}required{]} // must be initialized by hand\{\{nl\}\}\}\{\{nl\}\}\_ = Foo\{\} // Error, r must be initialized explicitly`} \tn % Row Count 9 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Structs: Trailing struct literal arguments}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`my\_fn(text: 'Click me', width: 100) // same as\{\{nl\}\}my\_fn(BtnCfg\{text: 'Click me', width: 100\})`\{\{nl\}\}Note if struct `BtnCfg` is annotated with `{[}params{]}`, `my\_fn` can be called without explicit parameters, as `my\_fn()` (default field values are used).} \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Structs: Methods}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`struct User \{ age int \}\{\{nl\}\}fn (u User) can\_register() bool // u == this / self \{\{nl\}\}\{ return u.age \textgreater{} 16 \}\{\{nl\}\}println(usr.can\_register()) // true`} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Structs: Heap structs}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Use {\bf{\&}} to alloc on the heap and get reference\{\{nl\}\}`p := \&Point\{10, 10\} // The type of p is \&Point\{\{nl\}\}println(p.x) // Same syntax for field access`} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`struct Foo \{\{\{nl\}\}~~mut:\{\{nl\}\}~~~~x int\{\{nl\}\}\}\{\{nl\}\}fa := Foo\{1\} // On stack\{\{nl\}\}mut a := fa // a is a {\bf{copy}} of fa\{\{nl\}\}a.x = 2\{\{nl\}\}assert fa.x == 1\{\{nl\}\}assert a.x == 2`} \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`mut fc := Foo\{1\} // Must be mut, as we\{\{nl\}\}mut c := \&fc // can't have mutable ref to immutable\{\{nl\}\}c.x = 2\{\{nl\}\}assert fc.x == 2\{\{nl\}\}println(c) // \&Foo\{ x: 2 \}, Note \textbackslash{}`\&\textbackslash{}``} \tn % Row Count 13 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Structs: Embedded}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`struct Size \{\{\{nl\}\}mut:\{\{nl\}\}~~width int\{\{nl\}\}~~height int\{\{nl\}\}\}\{\{nl\}\}fn (s \&Size) area() int \{ return s.width * s.height \}\{\{nl\}\}struct Button \{\{\{nl\}\}~~Size // {\bf{Size fields/methods embedded in Button}}\{\{nl\}\}~~title string\{\{nl\}\}\}\{\{nl\}\}mut button := Button \{\{\{nl\}\}~~title: 'Click me'\{\{nl\}\}~~height: 2\{\{nl\}\}\}\{\{nl\}\}button.width = 3\{\{nl\}\}assert button.area() == 6 // Use explicit struct name to avoid\{\{nl\}\}assert button.Size.area() == 6 // ambiguous fields / methods\{\{nl\}\}`} \tn % Row Count 11 (+ 11) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Initialize embedded struct:}}\{\{nl\}\}`mut button := Button \{\{\{nl\}\}~~Size: Size \{\{\{nl\}\}~~~~width: 3\{\{nl\}\}~~~~height: 2\{\{nl\}\}~~\}\{\{nl\}\}\}`} \tn % Row Count 15 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Or assign values:}}\{\{nl\}\}`button.Size = Size \{\{\{nl\}\}~~width: 4\{\{nl\}\}~~height: 5\{\{nl\}\}\}`} \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Numbers}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`a := 123 // Declare a new int variable, assigning 123`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`a := 0x7B // Same int variable in hexadecimal notation\{\{nl\}\}b := 0b01111011 // Same in binary notation\{\{nl\}\}c := 0o173 // Same in octal notation`} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\_ can be used as a separator: `1\_000\_000 0b0\_11 3\_122.55 0xF\_F 0o17\_3`} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{To declare variables of types other than int, use casting:\{\{nl\}\}`a := i64(123)\{\{nl\}\}b := u8(42)\{\{nl\}\}c := i16(12345)\{\{nl\}\}f := 1.0 // f64 is the default float type\{\{nl\}\}f1 := f64(3.14) // f64 is the default float type\{\{nl\}\}f2 := f32(3.14)\{\{nl\}\}f3 := 42e1 // 420\{\{nl\}\}f4 := 123e-2 // 1.23\{\{nl\}\}f5 := 456e+2 // 45600`} \tn % Row Count 14 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Strings}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`name := 'Bob'`\{\{nl\}\}`assert name.len == 3 // number of bytes`\{\{nl\}\}`name{[}0{]} == u8(66) // indexing gives a byte`\{\{nl\}\}`name{[}1..3{]} == 'ob' // slicing -\textgreater{} string 'ob'`\{\{nl\}\}`cr\_lf := '\textbackslash{}r\textbackslash{}n' // escape special chars like in C`\{\{nl\}\}`assert '\textbackslash{}xc0'{[}0{]} == u8(0xc0) // \textbackslash{}`\textbackslash{}x\#\#\textbackslash{}` hex number`\{\{nl\}\}`aardvark := '\textbackslash{}141ardvark' // \textbackslash{}\#\#\# octal`\{\{nl\}\}`star\_str := '\textbackslash{}u2605' // ★ Unicode as hex \textbackslash{}`\textbackslash{}u\#\#\#\#\textbackslash{}``\{\{nl\}\}`assert star\_str == '\textbackslash{}xe2\textbackslash{}x98\textbackslash{}x85' // UTF-8`} \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{For {\bf{raw strings}}, prepend {\bf{r}}. Escape handling is skipped:\{\{nl\}\}`s := r'hello\textbackslash{}nworld' // the \textbackslash{}`\textbackslash{}n\textbackslash{}` is two characters`} \tn % Row Count 12 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{String operators}}\{\{nl\}\}`bobby := 'Bob' + 'by' // "Bobby"\{\{nl\}\}mut s := 'hello '\{\{nl\}\}s += 'world' // "hello world"\{\{nl\}\}println('age = ' + 10.str()) // must be same type`} \tn % Row Count 16 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Converting strings to integers}}\{\{nl\}\}`'42'.int() == 42 // all\{\{nl\}\}'0xc3'.int() == 195 // int\{\{nl\}\}'0o10'.int() == 8 // literals\{\{nl\}\}'0b1111\_0000\_1010'.int() == 3850 // are\{\{nl\}\}'-0b1111\_0000\_1010'.int() == -3850 // supported`} \tn % Row Count 21 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Converting strings to runes}}\{\{nl\}\}`'Bob'.runes() // == {[}\textbackslash{}`B\textbackslash{}`, \textbackslash{}`o\textbackslash{}`, \textbackslash{}`b\textbackslash{}`{]}`} \tn % Row Count 23 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Converting strings to bytes}}\{\{nl\}\}`'Bob'.bytes() // == {[}u8(66), 111, 98{]}`} \tn % Row Count 25 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Array methods}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=35\}\}a.str() & converts a to string \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} a.clone() & produces a new array, containing a copy of data in the original array \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} a.filter(it \% 2 == 0) & produces a new array, containing a copy of elements of the original, that make the expression evaluate to true ({\bf{it}} refers to every element of the original array, consecutively). Can accept anonymous function:\{\{nl\}\}`a.filter(fn (x int) bool\{\{nl\}\}~~\{ return x \% 2 == 0 \})` \tn % Row Count 17 (+ 12) % Row 3 \SetRowColor{white} \seqsplit{a.map(it.to\_upper())} & produces a new array, each element is the value of the expression in parenthethes ({\bf{it}} refers to every element of the original array, consecutively). Can accept anonymous function:\{\{nl\}\}`a.map(fn (w string) string\{\{nl\}\}~~\{ return w.to\_upper() \})` \tn % Row Count 28 (+ 11) % Row 4 \SetRowColor{LightBackground} a.repeat(n) & concatenates the array elements n times \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Array methods (cont)}} \tn % Row 5 \SetRowColor{LightBackground} a.insert(i, val) & inserts a new element val at index i and shifts all following elements to the right \tn % Row Count 4 (+ 4) % Row 6 \SetRowColor{white} a.insert(i, {[}3, 4, 5{]}) & inserts several elements \tn % Row Count 6 (+ 2) % Row 7 \SetRowColor{LightBackground} a.prepend(val) & inserts a value at the beginning, equivalent to a.insert(0, val) \tn % Row Count 9 (+ 3) % Row 8 \SetRowColor{white} a.prepend(arr) & inserts elements of array arr at the beginning \tn % Row Count 11 (+ 2) % Row 9 \SetRowColor{LightBackground} \seqsplit{a.trim(new\_len)} & truncates the length (if new\_length \textless{} a.len, otherwise does nothing) \tn % Row Count 14 (+ 3) % Row 10 \SetRowColor{white} a.clear() & empties the array without changing cap (equivalent to a.trim(0)) \tn % Row Count 17 (+ 3) % Row 11 \SetRowColor{LightBackground} \seqsplit{a.delete\_many(start}, size) & removes size consecutive elements from index start – triggers reallocation \tn % Row Count 21 (+ 4) % Row 12 \SetRowColor{white} \seqsplit{a.delete(index)} & a.delete\_many(index, 1) \tn % Row Count 22 (+ 1) % Row 13 \SetRowColor{LightBackground} \seqsplit{a.delete\_last()} & removes the last element \tn % Row Count 23 (+ 1) % Row 14 \SetRowColor{white} a.first() & equivalent to a{[}0{]} \tn % Row Count 24 (+ 1) % Row 15 \SetRowColor{LightBackground} a.last() & equivalent to a{[}a.len - 1{]} \tn % Row Count 26 (+ 2) % Row 16 \SetRowColor{white} a.pop() & removes the last element and returns it \tn % Row Count 28 (+ 2) % Row 17 \SetRowColor{LightBackground} a.reverse() & makes a new array with the elements of a in reverse order \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.12 cm} x{4.88 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Array methods (cont)}} \tn % Row 18 \SetRowColor{LightBackground} \seqsplit{a.reverse\_in\_place()} & reverses the order of elements in a \tn % Row Count 2 (+ 2) % Row 19 \SetRowColor{white} a.join(joiner) & concatenates an array of strings into one string using joiner string as a separator \tn % Row Count 6 (+ 4) % Row 20 \SetRowColor{LightBackground} a.all(it == 2) & true if the expression evaluates to true for all elements of a. ({\bf{it}} refers to every element of the array, consecutively) \tn % Row Count 12 (+ 6) % Row 21 \SetRowColor{white} a.any(it \textgreater{} 2) & true if the expression evaluates to true for at least one element of a. ({\bf{it}} refers to every element of the array, consecutively) \tn % Row Count 18 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Array methods: Chaining}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`// using filter, map and negatives array slices\{\{nl\}\}files := {[}'pippo.jpg', '01.bmp', '\_v.txt', 'img\_02.jpg', 'img\_01.JPG'{]}\{\{nl\}\}filtrd := files.filter(it\#{[}-4..{]}.to\_lower() == '.jpg').map(it.to\_upper())\{\{nl\}\}// {[}'PIPPO.JPG', 'IMG\_02.JPG', 'IMG\_01.JPG'{]}`} \tn % Row Count 6 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Array slices}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A slice is a part of a parent array. Initially it refers to the elements between two indices separated by a .. operator. If a right-side index is absent, it is assumed to be the array length. If a left-side index is absent, it is assumed to be 0.\{\{nl\}\}{\bf{Slices are arrays}} themselves (they are not distinct types).} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`nums := {[}0, 10, 20, 30, 40{]}\{\{nl\}\}nums{[}1..4{]} // {[}10, 20, 30{]}\{\{nl\}\}nums{[}..4{]} // {[}0, 10, 20, 30{]}\{\{nl\}\}nums{[}1..{]} // {[}10, 20, 30, 40{]}`} \tn % Row Count 10 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A slice is always created with the smallest possible capacity {\bf{cap == len}}. Because of this, when the size of the slice increases, it is immediately reallocated and copied to another memory location, becoming independent from the parent array (copy on grow). In particular pushing elements to a slice does not alter the parent:} \tn % Row Count 17 (+ 7) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`mut a := {[}0, 1, 2, 3, 4, 5{]}\{\{nl\}\}mut b := a{[}2..4{]}\{\{nl\}\}b{[}0{]} = 7 // \textbackslash{}`b{[}0{]}\textbackslash{}` is referring to \textbackslash{}`a{[}2{]}\textbackslash{}`\{\{nl\}\}println(a) // {[}0, 1, 7, 3, 4, 5{]}\{\{nl\}\}b \textless{}\textless{} 9 // b is reallocated\{\{nl\}\}println(a) // {[}0, 1, 7, 3, 4, 5{]} no change\{\{nl\}\}println(b) // {[}7, 3, 9{]}`} \tn % Row Count 22 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Appending to the parent array may or may not make it independent from its child slices. The behaviour depends on the parent's capacity and is predictable:} \tn % Row Count 26 (+ 4) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`mut a := {[}{]}int\{len: 5, cap: 6, init: 2\}\{\{nl\}\}mut b := a{[}1..4{]}\{\{nl\}\}a \textless{}\textless{} 3 // no realloc - fits in cap\{\{nl\}\}b{[}2{]} = 13 // a{[}3{]} is modified\{\{nl\}\}a \textless{}\textless{} 4 // cap exceeded, reallocated\{\{nl\}\}b{[}1{]} = 3 // no change in {\bf{a}}\{\{nl\}\}println(a) // {[}2, 2, 2, 13, 2, 3, 4{]}\{\{nl\}\}println(b) // {[}2, 3, 13{]}`} \tn % Row Count 32 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Array slices (cont)}} \tn % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Call {\bf{.clone()}} on the slice, if you want an independent copy right away:} \tn % Row Count 2 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`mut a := {[}0, 1, 2, 3, 4, 5{]}\{\{nl\}\}mut b := a{[}2..4{]}.clone()\{\{nl\}\}b{[}0{]} = 7 // NOT referring to a{[}2{]}\{\{nl\}\}println(a) // {[}0, 1, 2, 3, 4, 5{]}\{\{nl\}\}println(b) // {[}7, 3{]}`} \tn % Row Count 6 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Array slices: Negative indexing}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Negative indexing}} starts from the end of the array towards the start, for example {\bf{-3}} is equal to {\bf{array.len - 3}}. Negative slices have a different syntax: {\bf{a\#{[}..-3{]}}}. The result is "locked" inside the array.The returned slice is always a valid array, though it may be empty:} \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`a := {[}0, 1, 2, 3, 4, 5{]}\{\{nl\}\}a\#{[}-3..{]} // {[}3, 4, 5{]}\{\{nl\}\}a\#{[}-20..{]} // {[}0, 1, 2, 3, 4, 5{]}\{\{nl\}\}a\#{[}-20..-3{]} // {[}0, 1, 2{]}\{\{nl\}\}a\#{[}..-1{]} // {[}0, 1, 2, 3, 4{]}\{\{nl\}\}// {\bf{Empty arrays}}\{\{nl\}\}a\#{[}-20..-6{]} // {[}{]}\{\{nl\}\}a\#{[}20..10{]} // {[}{]}\{\{nl\}\}a\#{[}20..30{]} // {[}{]}`} \tn % Row Count 11 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Maps}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Keys can be {\bf{string}}s, {\bf{rune}}s, {\bf{integer}}s, {\bf{float}}s or {\bf{voidptr}}s.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`mut m := map{[}string{]}int\{\} // `string` keys and `int` values\{\{nl\}\}m{[}'one'{]} = 1\{\{nl\}\}m{[}'two'{]} = 2\{\{nl\}\}println(m{[}'one'{]}) // "1"\{\{nl\}\}println(m{[}'bad\_key'{]}) // "0"\{\{nl\}\}println('bad\_key' in m) // \textbackslash{}`{\bf{in}}\textbackslash{}` to detect if such key exists\{\{nl\}\}println(m.keys()) // {[}'one', 'two'{]}\{\{nl\}\}m.delete('two')`} \tn % Row Count 8 (+ 6) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Map can be initialized using this short syntax:\{\{nl\}\}`numbers := \{ 'one': 1 'two': 2 \}`} \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{If a key is not found, a zero value is returned by default:\{\{nl\}\}`println(\{ 'abc': 'xyz' \}{[}'bad key'{]}) // ''\{\{nl\}\}println(\{ 1: 123, 2: 456 \}{[}3{]}) // 0`} \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Or use an {\bf{or \{\}}} block to handle missing keys:\{\{nl\}\}`mm := map{[}string{]}int\{\}\{\{nl\}\}val := mm{[}'bad\_key'{]} or \{ panic('key not found') \}`} \tn % Row Count 16 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Check if a key is present and get its value (if present):\{\{nl\}\}`m := \{ 'abc': 'def' \}\{\{nl\}\}if v := m{[}'abc'{]} \{ println('the map value for that key is: \$v') \}`} \tn % Row Count 20 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Unions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Just like structs, unions support embedding.\{\{nl\}\}`struct Rgba32\_Component \{\{\{nl\}\}~~r byte\{\{nl\}\}~~b byte\{\{nl\}\}~~g byte\{\{nl\}\}~~a byte\{\{nl\}\}\}\{\{nl\}\}union Rgba32 \{\{\{nl\}\}~~Rgba32\_Component\{\{nl\}\}~~value u32\{\{nl\}\}\}\{\{nl\}\}clr1 := Rgba32 \{ value: 0x008811FF \}\{\{nl\}\}clr2 := Rgba32 \{\{\{nl\}\}~~Rgba32\_Component: Rgba32\_Component \{ a: 128 \}\{\{nl\}\}\}\{\{nl\}\}sz := sizeof(Rgba32)\{\{nl\}\}unsafe \{ // Union member access must be in unsafe block\{\{nl\}\}~~println('Size: \$\{sz\}B, clr1.b: \$clr1.b, clr2.b: \$clr2.b')\{\{nl\}\}\}`} \tn % Row Count 12 (+ 12) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Note that the embedded struct arguments are not necessarily stored in the order listed.} \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}