diff --git a/.gitignore b/.gitignore index 551225c0..9ffa1d8d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ hugo.yaml .hugo_build.lock .gitmodules -/docs /data /themes /static \ No newline at end of file diff --git a/README.md b/README.md index 28cc9876..65172992 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,6 @@ * [Lifetimes](content/en/docs/c3.lifetimes.md) ## Lets Get It Started -* [Code organization](content/en/docs/d1.code-organization.md) -* [Functions](content/en/docs/d2.functions.md) * [Modules](content/en/docs/d3.modules.md) * [Crates](content/en/docs/d4.crates.md) * [Workspaces](content/en/docs/d5.workspaces.md) diff --git a/docs/assets/css/docs.min.40875d326eab7be4be3f43f39f9edd1b487bf0ecf4482bf63d05ab94ac771c3e.css b/docs/assets/css/docs.min.40875d326eab7be4be3f43f39f9edd1b487bf0ecf4482bf63d05ab94ac771c3e.css new file mode 100644 index 00000000..0d03bdc1 --- /dev/null +++ b/docs/assets/css/docs.min.40875d326eab7be4be3f43f39f9edd1b487bf0ecf4482bf63d05ab94ac771c3e.css @@ -0,0 +1,5 @@ +*:where(:not(html,iframe,canvas,img,svg,video,audio,pre,code):not(svg *,symbol *)){all:unset;display:revert}*,*::before,*::after{box-sizing:border-box}html{-moz-text-size-adjust:none;-webkit-text-size-adjust:none;text-size-adjust:none}a,button{cursor:revert}ol,ul,menu,summary{list-style:none}ol{counter-reset:revert}img{max-inline-size:100%;max-block-size:100%}table{border-collapse:collapse}input,textarea{-webkit-user-select:auto}textarea{white-space:revert}meter{-webkit-appearance:revert;appearance:revert}:where(pre){all:revert;box-sizing:border-box}::placeholder{color:unset}::marker{content:initial}:where([hidden]){display:none}:where([contenteditable]:not([contenteditable=false])){-moz-user-modify:read-write;-webkit-user-modify:read-write;overflow-wrap:break-word;-webkit-line-break:after-white-space;-webkit-user-select:auto}:where([draggable=true]){-webkit-user-drag:element}:where(dialog:modal){all:revert;box-sizing:border-box}pre,code{margin:0}::-webkit-details-marker{display:none}:root{--font-sans:ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";--font-serif:ui-serif, Georgia, Cambria, "Times New Roman", Times, serif;--font-mono:ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;--font-brand:"Arial", serif;--color:rgba(0, 0, 0, 1);--color-fade:rgba(0, 0, 0, .54);--color-nav:rgba(0, 0, 0, .7);--background:#ffffff;--background-grey:#f9f9f9;--background-grey-embed:#f0f0f0;--foreground:rgba(247, 247, 247, .25);--foreground-hover:rgba(220, 220, 220, .35);--foreground-highlight:#f5d41f;--color-highlight:#130606;--model-background:#ffffff;--model-foreground:rgba(247, 247, 247, .25);--model-box-shadow:0 8px 32px 0 rgba(31, 38, 135, .37);--padding:12px;--padding_d2:6px;--padding_x2:24px;--model-blur:10px;--border:#efefef;--border-nav:#dddddd;--radius:4px;--radius-round:24px;--anchor:#235ce8;--hover:#547ce3;--chroma-base00:#f9f9f9;--chroma-base01:#e0e0e0;--chroma-base02:rgba(159, 218, 159, .2);--chroma-base03:#8e908c;--chroma-base04:#969896;--chroma-base05:#4d4d4c;--chroma-base06:#282a2e;--chroma-base07:#1d1f21;--chroma-base08:#c82829;--chroma-base09:#f5871f;--chroma-base0A:#eab700;--chroma-base0B:#718c00;--chroma-base0C:#3e999f;--chroma-base0D:#4271ae;--chroma-base0E:#8959a8;--chroma-base0F:#a3685a;--callout-info-backgound:#954ce3;--callout-info-foreground:rgba(0, 0, 0, .08);--callout-info-color:#ffffff;--callout-info-anchor:#d2aefc;--callout-info-anchor-text-shadow:.4px 0 .8px black;--cover-img-height:36svh;--section-item-cover-img-height:20svh}:root[data-color=dark]{--color:rgba(255, 255, 255, 1);--color-fade:rgba(255, 255, 255, .5);--color-nav:rgba(255, 255, 255, .8);--background:#101010;--background-grey:#1e1e1e;--background-grey-embed:#2f2d2d;--foreground:rgba(19, 19, 19, 1);--foreground-hover:rgba(35, 35, 35, 1);--model-background:rgba(16, 16, 16, 1);--model-foreground:rgba(225, 225, 225, .05);--model-box-shadow:0 8px 32px 0 rgba(241, 235, 235, 0.1);--border:rgba(225, 225, 225, .1);--border-nav:rgba(255, 255, 255, .2);--anchor:#2e87f1;--hover:#044b9d;--chroma-base00:var(--background-grey);--chroma-base01:#393939;--chroma-base02:rgba(159, 218, 159, .1);--chroma-base03:#999999;--chroma-base04:#b4b7b4;--chroma-base05:#cccccc;--chroma-base06:#e0e0e0;--chroma-base07:#ffffff;--chroma-base08:#f2777a;--chroma-base09:#f99157;--chroma-base0A:#ffcc66;--chroma-base0B:#99cc99;--chroma-base0C:#66cccc;--chroma-base0D:#6699cc;--chroma-base0E:#cc99cc;--chroma-base0F:#a3685a}@view-transition{navigation: auto; +}.site-logo{font-family:var(--font-brand);font-size:1.5em;color:transparent;-webkit-text-stroke:.5px var(--border);background:0 0;letter-spacing:3px;background:linear-gradient(45deg,#866ee7,#ea60da,#ed8f57,#fbd41d,#2cca91);-webkit-background-clip:text;-webkit-text-fill-color:transparent;background-clip:text;font-weight:800}@media(min-width:768px){:root{--cover-img-height:46svh;--section-item-cover-img-height:27svh}}@media(min-width:1024px){:root{--padding:16px;--padding_d2:8px;--padding_x2:32px;--cover-img-height:53svh;--section-item-cover-img-height:28svh}}@media(min-width:1024px) and (orientation:portrait){:root{--cover-img-height:34svh;--section-item-cover-img-height:16svh}}@media(min-width:1280px){:root{--cover-img-height:67svh;--section-item-cover-img-height:22svh}}.btn{display:flex;padding:var(--padding_d2);background:var(--foreground);border:1px solid var(--border);border-radius:var(--radius);gap:var(--padding_d2);cursor:pointer;color:var(--color-nav)}.btn:hover,.btn:focus{background:var(--foreground-hover)}select,::picker(select){appearance:base-select}select{display:flex;padding:var(--padding_d2);background:var(--foreground);border:1px solid var(--border);border-radius:var(--radius);gap:var(--padding_d2);cursor:pointer;color:var(--color-nav)}select:hover,select:focus{background:var(--foreground-hover);color:var(--color-nav)}select::picker-icon{content:"⌵";font-size:.8rem}select:open::picker-icon{rotate:180deg;transition:.3s}option{display:flex;justify-content:flex-start;padding:var(--padding_d2);background:var(--foreground);border:1px solid var(--border);gap:var(--padding_d2);transition:.6s;color:var(--color-nav)}option:first-of-type{border-radius:var(--radius)var(--radius)0 0}option:last-of-type{border-radius:0 0 var(--radius)var(--radius)}option:not(option:last-of-type){border-bottom:none}option:nth-of-type(even){background:var(--background-grey)}option:hover,option:focus{background:var(--foreground-hover)}option .icon{text-box:trim-both cap alphabetic}selectedcontent .icon{display:none}option:checked{background:var(--background-grey-embed)}option::checkmark{order:1;margin-left:auto;content:"✅ "}::picker(select){border:none;border-radius:var(--radius);top:calc(anchor(bottom) + 1px);left:calc(-1*var(--padding));opacity:0;transition:all .2s allow-discrete}::picker(select):popover-open{opacity:1}@starting-style{::picker(select):popover-open { + opacity: 0; + } +}html{scroll-behavior:smooth;scroll-padding:2em}body{display:flex;flex-direction:column;height:100%;color:var(--color);background:var(--background);font-family:var(--font-sans)}#outer-wrapper{display:flex}#aside-wrapper{width:85%;left:-85%;display:none;overflow-x:auto}#aside-wrapper>aside>div{display:flex;padding:var(--padding);justify-content:flex-end}#aside-wrapper>aside>a{display:none}#content-wrapper{display:flex;flex:1;flex-direction:column;min-height:100svh}main{display:flex;flex-direction:column;flex:1;width:100svw}#aside-wrapper.open{display:flex;flex-direction:column;position:fixed;top:0;height:100%;z-index:10;transition:.3s;animation:slide-in-left .3s forwards;background:var(--model-background);box-shadow:var(--model-box-shadow)}@keyframes slide-in-left{from{transform:translateX(0)}to{transform:translateX(100%)}}#content-wrapper>header{border-bottom:1px solid var(--border)}#content-wrapper>header>a{display:inline-block;padding:var(--padding)}#content-wrapper>footer{display:flex;flex-direction:column;padding:var(--padding_x2)var(--padding)0}#content-wrapper>footer>div{display:flex;align-items:center;justify-content:center;gap:var(--padding_d2);text-align:center;flex-wrap:wrap}#content-wrapper>footer>div:first-child{white-space:initial;padding:var(--padding)}#content-wrapper>footer>div:first-child a{background:var(--foreground-highlight);color:var(--color-highlight);border-radius:var(--radius);cursor:pointer;padding:0 var(--padding_d2)}#content-wrapper>footer>div:nth-child(2){justify-content:flex-end;padding:var(--padding)0}#content-wrapper>nav{display:flex;justify-content:end}@media(min-width:768px){#content-wrapper>header{border-bottom:none}#content-wrapper>nav>select{position:absolute;top:calc(var(--padding) - 4px);right:var(--padding)}}@media(min-width:1280px){#content-wrapper>nav>select{right:calc(100vw/5 + calc(1.5*var(--padding_x2)))}main>article{padding-top:var(--padding_x2)}}main>article{display:flex;flex:1;flex-direction:column}main>article>nav{display:flex;padding:var(--padding_x2)var(--padding)var(--padding);gap:12px;justify-content:space-between}main>article>header{padding:var(--padding)var(--padding_x2)}#article-body{display:flex;flex-direction:column;flex:1;padding:var(--padding)var(--padding_x2);overflow-x:auto}main>article>footer{display:grid;grid-template-columns:repeat(2,1fr);padding:0 var(--padding_x2);row-gap:var(--padding)}main>article>footer>time{grid-column:1/3;display:flex;color:var(--color-fade);justify-content:center;padding:var(--padding)0;gap:var(--padding_d2)}main>article>footer>a{display:flex;padding:var(--padding);gap:calc(var(--padding)/4);background:var(--foreground);color:var(--color-nav);border:1px solid var(--border);border-radius:var(--radius);cursor:pointer;width:75%}main>article>footer>a:hover{background:var(--foreground-hover)}main>article>footer>a.hidden{visibility:hidden}main>article>footer>a:last-child{display:flex;justify-content:end;margin-left:25%}main>aside{width:85%;right:-85%;display:none;overflow-x:auto}main>aside.open{display:flex;flex-direction:column;position:fixed;top:0;height:100%;z-index:10;transition:.3s;animation:slide-in-right .3s forwards;background:var(--model-background);box-shadow:var(--model-box-shadow)}#aside-wrapper{position:sticky;top:0}#aside-wrapper>aside>nav{padding:0 var(--padding)}#aside-wrapper>aside>nav>details{padding-bottom:0}#aside-wrapper>aside>nav>details[open]{padding-bottom:calc(var(--padding) * 1.5)}#aside-wrapper>aside>nav>details>summary{display:flex;padding:var(--padding)0;color:var(--color);font-weight:700}#aside-wrapper>aside>nav>details[open]>summary{color:var(--color-fade)}#aside-wrapper>aside>nav>details>ul>li{margin:0 var(--padding_d2);border-left:1px solid var(--border-nav)}#aside-wrapper>aside>nav>details>ul>li>a{color:var(--color-nav);display:flex;cursor:pointer;padding:var(--padding_d2)}#aside-wrapper>aside>nav>details>ul>li>a::before{content:'';display:inline-block;background:var(--background);width:var(--padding_d2);height:var(--padding_d2);transform:rotate(45deg);border:1px solid var(--border-nav);position:relative;left:calc((var(--padding) * -.78));top:3px}#aside-wrapper>aside>nav>details>ul>li>a.active{background:var(--foreground-highlight);color:var(--color-highlight);border-top-right-radius:var(--radius);border-bottom-right-radius:var(--radius)}#aside-wrapper>aside>nav>details>ul>li>a.active::before{background:var(--foreground-highlight);border:var(--foreground-highlight);left:calc((var(--padding) * -.76))}#aside-wrapper>aside>nav>details>ul>li>a:hover:not(.active){background:var(--foreground-hover);border-top-right-radius:var(--radius);border-bottom-right-radius:var(--radius)}main>aside>div{display:flex;padding:var(--padding)}main>aside>strong{display:flex;padding:var(--padding);color:var(--color-fade);font-weight:700}main>aside>nav{padding:0 var(--padding)}#TableOfContents ul{border-left:1px solid var(--border)}#TableOfContents a{color:var(--color-nav);display:inline-block;cursor:pointer;padding:var(--padding_d2)}#TableOfContents a code{padding:calc(var(--padding)/3.7);border-radius:var(--radius);font-size:.9em}#TableOfContents ul a::before{content:'';display:inline-block;width:calc(var(--padding)/1.5);height:calc(var(--padding)/1.5);background:var(--background);border:1px solid var(--border-nav);position:relative;left:calc((var(--padding) * -.89));top:-3px;border-radius:30%;transform:rotate(45deg)}#TableOfContents a.active{background:var(--foreground-highlight);color:var(--color-highlight);border-top-right-radius:var(--radius);border-bottom-right-radius:var(--radius)}#TableOfContents ul a.active::before{background:var(--background)}#TableOfContents a:hover:not(.active){background:var(--foreground-hover);border-top-right-radius:var(--radius);border-bottom-right-radius:var(--radius)}#TableOfContents ul ul{margin-left:calc(var(--padding) * 1.75)}@keyframes slide-in-right{from{transform:translateX(0)}to{transform:translateX(-100%)}}body.model-open{overflow:hidden}#body-model-outer{display:none;content:"";position:fixed;top:0;left:0;width:100%;height:100%;z-index:5;background:var(--model-foreground);backdrop-filter:blur(4px);-webkit-backdrop-filter:blur(4px)}#aside-wrapper>aside>div .btn,main>aside>div>.btn{font-size:.75em;font-weight:800}@media(min-width:768px){main>article>footer>a{width:50%}main>article>footer>a:last-child{margin-left:50%}}@media(min-width:768px) and (max-width:1023px){#aside-wrapper{width:50%;left:-50%}main>aside{width:50%;right:-50%}}@media(min-width:1024px){main{flex-direction:row}main>article{width:75vw;overflow-x:auto}main>aside{width:25%;display:flex;flex-direction:column;position:sticky;top:0;height:100svh;overflow-x:auto}main>aside>div{display:none}main>article>nav>button:last-child{display:none}#content-wrapper>footer{padding:var(--padding)var(--padding)0;flex-direction:row}#content-wrapper>footer>div:first-child{width:75vw}#content-wrapper>footer>div:nth-child(2){width:25%}}@media(min-width:1024px) and (max-width:1279px){#aside-wrapper{width:33%;left:-33%}}@media(min-width:1280px){#aside-wrapper{width:20%;display:flex;flex-direction:column;height:100svh;background:var(--foreground);border-right:1px solid var(--border);overflow-x:auto}#aside-wrapper>aside>div{display:none}#aside-wrapper>aside>nav{overflow-x:auto}#content-wrapper>header{display:none}#aside-wrapper>aside>a{display:flex;padding:var(--padding)}main{width:80svw;padding:0 var(--padding_x2)}main>article{width:60vw;overflow-x:auto}main>article>nav{display:none}main>aside{width:25%}main>article>header{padding:var(--padding_x2)var(--padding_x2)var(--padding)}#content-wrapper>footer>div:first-child{width:60vw}#content-wrapper>footer>div:nth-child(2){width:25%}}main>article>header>h1{font-size:3em}main>article>header>p{font-size:1.25em;color:var(--color-fade);padding-top:var(--padding)}#article-body h1,#article-body h2,#article-body h3,#article-body h4,#article-body h5,#article-body h6{line-height:1em;font-weight:400;margin:2.6em 0 .1em;color:var(--color)}#article-body h1{font-size:1.8em}#article-body h2{font-size:1.5em}#article-body h3{font-size:1.3em}#article-body h4{font-size:1.1em}#article-body h2:first-child{margin-top:.6em}#article-body .highlight,#article-body blockquote,#article-body dl,#article-body iframe,#article-body ol,#article-body p,#article-body table,#article-body ul{margin-top:1em;line-height:1.8rem;letter-spacing:-.1px}#article-body blockquote p{margin:1em 0}#article-body blockquote dl,#article-body blockquote ol,#article-body blockquote ul{margin:0 1em 1em}#article-body a{color:var(--anchor);text-decoration:none}#article-body a:hover{color:var(--hover);text-decoration:underline}#article-body strong,#article-body b,#article-body table th{font-weight:600}#article-body em{font-style:italic}#article-body dl,#article-body ol,#article-body ul{margin-left:20px}#article-body dl dl,#article-body dl ol,#article-body dl ul,#article-body ol dl,#article-body ol ol,#article-body ol ul,#article-body ul dl,#article-body ul ol,#article-body ul ul{margin-top:0;margin-bottom:0}#article-body ul{list-style:disc}#article-body ol{list-style:decimal}#article-body dl{list-style:square}#article-body li>ul{list-style:circle}#article-body li>ol{list-style:lower-alpha}#article-body li p{margin:0}#article-body li .highlight,#article-body li blockquote,#article-body li iframe,#article-body li table{margin:1em 0}#article-body img,#article-body video{max-width:100%;border-radius:4px}#article-body hr:before{display:block;text-align:center;content:"∙ ∙ ∙";color:var(--color-fade);letter-spacing:.6em;top:calc(var(--padding)/3.7);margin:2.6em 0 1em}#article-body blockquote{padding:8px 12px;position:relative;background:var(--background-grey);border-left-width:5px;border-radius:6px}#article-body blockquote footer{margin:1em 0;font-style:italic}#article-body blockquote footer cite:before{content:"—";padding:0 .3em}#article-body blockquote footer cite a{color:var(--border)}#article-body code,#article-body pre{font-family:var(--font-mono)}#article-body h1 code,#article-body h2 code,#article-body h3 code,#article-body h4 code,#article-body h5 code,#article-body h6 code,#article-body p code,#article-body blockquote code,#article-body ul code,#article-body ol code,#article-body dl code,#article-body table code{background:var(--background-grey);padding:calc(var(--padding)/3.7);border-radius:var(--radius);font-size:.9em}#article-body blockquote code{background:var(--background-grey-embed)}#article-body pre:not(.chroma){color:var(--chroma-base05);font-size:.9em;line-height:1.8;letter-spacing:-.1px;background-color:var(--chroma-base00);border-radius:6px;padding:16px 24px;overflow-x:auto;margin-top:1em}#article-body blockquote .chroma,#article-body blockquote pre:not(.chroma){background:var(--foreground-hover);margin-bottom:1em}#article-body blockquote .chroma code,#article-body blockquote pre:not(.chroma) code{padding:0}#article-body li>.highlight>.chroma>code{display:block;background:0 0}#article-body table{max-width:100%;border-radius:var(--radius);box-shadow:0 0 0 1px var(--border)}#article-body table thead th:first-child{border-top-left-radius:var(--radius)}#article-body table thead th:last-child{border-top-right-radius:var(--radius)}#article-body table tbody tr:last-child{border-bottom-left-radius:var(--radius);border-bottom-right-radius:var(--radius)}#article-body table tbody tr:last-child td:first-child{border-bottom-left-radius:var(--radius)}#article-body table tbody tr:last-child td:last-child{border-bottom-right-radius:var(--radius)}#article-body table td,#article-body table th{padding:5px 15px}#article-body table tr:nth-child(2n){background:var(--background-grey)}#article-body table thead tr{background:var(--background-grey-embed)}.chroma{font-size:.9em;color:var(--chroma-base05);background-color:var(--chroma-base00);border-radius:6px;padding:16px 24px;overflow-x:auto}.chroma .x{color:var(--chroma-base05)}.chroma .err{color:var(--chroma-base08)}.chroma .lntd{vertical-align:top;padding:0;margin:0;border:0}.chroma .lntable{border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block}.chroma .hl{display:block;width:100%;background-color:var(--chroma-base02)}.chroma .lnt{margin-right:.4em;padding:0 .4em}.chroma .ln{margin-right:.4em;padding:0 .4em;border-right:1px solid var(--chroma-base0A)}.chroma .line{display:flex}.chroma .k{color:var(--chroma-base0E)}.chroma .kc{color:var(--chroma-base0E)}.chroma .kd{color:var(--chroma-base0E)}.chroma .kn{color:var(--chroma-base0E)}.chroma .kp{color:var(--chroma-base0D)}.chroma .kr{color:var(--chroma-base0E)}.chroma .kt{color:var(--chroma-base0E)}.chroma .n{color:var(--chroma-base05)}.chroma .na{color:var(--chroma-base05)}.chroma .nb{color:var(--chroma-base0D)}.chroma .bp{color:var(--chroma-base0D)}.chroma .nc{color:var(--chroma-base0A)}.chroma .no{color:var(--chroma-base09)}.chroma .nd{color:var(--chroma-base09)}.chroma .ni{color:var(--chroma-base0A)}.chroma .ne{color:var(--chroma-base0A)}.chroma .nf{color:var(--chroma-base05)}.chroma .fm{color:var(--chroma-base05)}.chroma .nl{color:var(--chroma-base08)}.chroma .nn{color:var(--chroma-base0A)}.chroma .nx{color:var(--chroma-base0D)}.chroma .py{color:var(--chroma-base08)}.chroma .nt{color:var(--chroma-base0D)}.chroma .nv{color:var(--chroma-base0D)}.chroma .vc{color:var(--chroma-base0D)}.chroma .vg{color:var(--chroma-base0D)}.chroma .vi{color:var(--chroma-base08)}.chroma .vm{color:var(--chroma-base0D)}.chroma .l{color:var(--chroma-base0B)}.chroma .ld{color:var(--chroma-base0B)}.chroma .s{color:var(--chroma-base0B)}.chroma .sa{color:var(--chroma-base0B)}.chroma .sb{color:var(--chroma-base0B)}.chroma .sc{color:var(--chroma-base0B)}.chroma .dl{color:var(--chroma-base0F)}.chroma .sd{color:var(--chroma-base03)}.chroma .s2{color:var(--chroma-base0B)}.chroma .se{color:var(--chroma-base0C)}.chroma .sh{color:var(--chroma-base0B)}.chroma .si{color:var(--chroma-base0F)}.chroma .sx{color:var(--chroma-base0B)}.chroma .sr{color:var(--chroma-base0C)}.chroma .s1{color:var(--chroma-base0B)}.chroma .ss{color:var(--chroma-base0B)}.chroma .m{color:var(--chroma-base09)}.chroma .mb{color:var(--chroma-base09)}.chroma .mf{color:var(--chroma-base09)}.chroma .mh{color:var(--chroma-base09)}.chroma .mi{color:var(--chroma-base09)}.chroma .il{color:var(--chroma-base09)}.chroma .mo{color:var(--chroma-base09)}.chroma .o{color:var(--chroma-base05)}.chroma .ow{color:var(--chroma-base05)}.chroma .p{color:var(--chroma-base05)}.chroma .c{color:var(--chroma-base03)}.chroma .ch{color:var(--chroma-base03)}.chroma .cm{color:var(--chroma-base03)}.chroma .c1{color:var(--chroma-base03)}.chroma .cs{color:var(--chroma-base03)}.chroma .cp{color:var(--chroma-base0F)}.chroma .cpf{color:var(--chroma-base0B)}.chroma .g{color:var(--chroma-base05)}.chroma .gd{color:var(--chroma-base08)}.chroma .ge{color:var(--chroma-base05);font-style:italic}.chroma .gr{color:var(--chroma-base05)}.chroma .gh{color:var(--chroma-base0D)}.chroma .gi{color:var(--chroma-base0B)}.chroma .go{color:var(--chroma-base05)}.chroma .gp{color:var(--chroma-base05)}.chroma .gs{color:var(--chroma-base05);font-weight:700}.chroma .gu{color:var(--chroma-base0D)}.chroma .gt{color:var(--chroma-base05)}.chroma .gl{color:var(--chroma-base05);text-decoration:underline}.chroma .w{color:var(--chroma-base00)}#article-body blockquote.info{border:none;background:var(--callout-info-backgound);color:var(--callout-info-color)}#article-body blockquote.info>p{display:flex;gap:var(--padding_d2)}#article-body blockquote.info>p:first-child>strong:first-child{font-size:2.5em}#article-body blockquote.info li{margin:0 var(--padding_d2)}#article-body blockquote.info code{background:var(--callout-info-foreground)}#article-body blockquote.info a{color:var(--callout-info-anchor);text-shadow:var(--callout-info-anchor-text-shadow)} \ No newline at end of file diff --git a/docs/assets/css/home.min.6df8c7826eeeba8fbaa32ccfd8d8983311af2769567f40622751f957d54ea722.css b/docs/assets/css/home.min.6df8c7826eeeba8fbaa32ccfd8d8983311af2769567f40622751f957d54ea722.css new file mode 100644 index 00000000..1fe8865b --- /dev/null +++ b/docs/assets/css/home.min.6df8c7826eeeba8fbaa32ccfd8d8983311af2769567f40622751f957d54ea722.css @@ -0,0 +1,5 @@ +*:where(:not(html,iframe,canvas,img,svg,video,audio,pre,code):not(svg *,symbol *)){all:unset;display:revert}*,*::before,*::after{box-sizing:border-box}html{-moz-text-size-adjust:none;-webkit-text-size-adjust:none;text-size-adjust:none}a,button{cursor:revert}ol,ul,menu,summary{list-style:none}ol{counter-reset:revert}img{max-inline-size:100%;max-block-size:100%}table{border-collapse:collapse}input,textarea{-webkit-user-select:auto}textarea{white-space:revert}meter{-webkit-appearance:revert;appearance:revert}:where(pre){all:revert;box-sizing:border-box}::placeholder{color:unset}::marker{content:initial}:where([hidden]){display:none}:where([contenteditable]:not([contenteditable=false])){-moz-user-modify:read-write;-webkit-user-modify:read-write;overflow-wrap:break-word;-webkit-line-break:after-white-space;-webkit-user-select:auto}:where([draggable=true]){-webkit-user-drag:element}:where(dialog:modal){all:revert;box-sizing:border-box}pre,code{margin:0}::-webkit-details-marker{display:none}:root{--font-sans:ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";--font-serif:ui-serif, Georgia, Cambria, "Times New Roman", Times, serif;--font-mono:ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;--font-brand:"Arial", serif;--color:rgba(0, 0, 0, 1);--color-fade:rgba(0, 0, 0, .54);--color-nav:rgba(0, 0, 0, .7);--background:#ffffff;--background-grey:#f9f9f9;--background-grey-embed:#f0f0f0;--foreground:rgba(247, 247, 247, .25);--foreground-hover:rgba(220, 220, 220, .35);--foreground-highlight:#f5d41f;--color-highlight:#130606;--model-background:#ffffff;--model-foreground:rgba(247, 247, 247, .25);--model-box-shadow:0 8px 32px 0 rgba(31, 38, 135, .37);--padding:12px;--padding_d2:6px;--padding_x2:24px;--model-blur:10px;--border:#efefef;--border-nav:#dddddd;--radius:4px;--radius-round:24px;--anchor:#235ce8;--hover:#547ce3;--chroma-base00:#f9f9f9;--chroma-base01:#e0e0e0;--chroma-base02:rgba(159, 218, 159, .2);--chroma-base03:#8e908c;--chroma-base04:#969896;--chroma-base05:#4d4d4c;--chroma-base06:#282a2e;--chroma-base07:#1d1f21;--chroma-base08:#c82829;--chroma-base09:#f5871f;--chroma-base0A:#eab700;--chroma-base0B:#718c00;--chroma-base0C:#3e999f;--chroma-base0D:#4271ae;--chroma-base0E:#8959a8;--chroma-base0F:#a3685a;--callout-info-backgound:#954ce3;--callout-info-foreground:rgba(0, 0, 0, .08);--callout-info-color:#ffffff;--callout-info-anchor:#d2aefc;--callout-info-anchor-text-shadow:.4px 0 .8px black;--cover-img-height:36svh;--section-item-cover-img-height:20svh}:root[data-color=dark]{--color:rgba(255, 255, 255, 1);--color-fade:rgba(255, 255, 255, .5);--color-nav:rgba(255, 255, 255, .8);--background:#101010;--background-grey:#1e1e1e;--background-grey-embed:#2f2d2d;--foreground:rgba(19, 19, 19, 1);--foreground-hover:rgba(35, 35, 35, 1);--model-background:rgba(16, 16, 16, 1);--model-foreground:rgba(225, 225, 225, .05);--model-box-shadow:0 8px 32px 0 rgba(241, 235, 235, 0.1);--border:rgba(225, 225, 225, .1);--border-nav:rgba(255, 255, 255, .2);--anchor:#2e87f1;--hover:#044b9d;--chroma-base00:var(--background-grey);--chroma-base01:#393939;--chroma-base02:rgba(159, 218, 159, .1);--chroma-base03:#999999;--chroma-base04:#b4b7b4;--chroma-base05:#cccccc;--chroma-base06:#e0e0e0;--chroma-base07:#ffffff;--chroma-base08:#f2777a;--chroma-base09:#f99157;--chroma-base0A:#ffcc66;--chroma-base0B:#99cc99;--chroma-base0C:#66cccc;--chroma-base0D:#6699cc;--chroma-base0E:#cc99cc;--chroma-base0F:#a3685a}@view-transition{navigation: auto; +}.site-logo{font-family:var(--font-brand);font-size:1.5em;color:transparent;-webkit-text-stroke:.5px var(--border);background:0 0;letter-spacing:3px;background:linear-gradient(45deg,#866ee7,#ea60da,#ed8f57,#fbd41d,#2cca91);-webkit-background-clip:text;-webkit-text-fill-color:transparent;background-clip:text;font-weight:800}@media(min-width:768px){:root{--cover-img-height:46svh;--section-item-cover-img-height:27svh}}@media(min-width:1024px){:root{--padding:16px;--padding_d2:8px;--padding_x2:32px;--cover-img-height:53svh;--section-item-cover-img-height:28svh}}@media(min-width:1024px) and (orientation:portrait){:root{--cover-img-height:34svh;--section-item-cover-img-height:16svh}}@media(min-width:1280px){:root{--cover-img-height:67svh;--section-item-cover-img-height:22svh}}.btn{display:flex;padding:var(--padding_d2);background:var(--foreground);border:1px solid var(--border);border-radius:var(--radius);gap:var(--padding_d2);cursor:pointer;color:var(--color-nav)}.btn:hover,.btn:focus{background:var(--foreground-hover)}select,::picker(select){appearance:base-select}select{display:flex;padding:var(--padding_d2);background:var(--foreground);border:1px solid var(--border);border-radius:var(--radius);gap:var(--padding_d2);cursor:pointer;color:var(--color-nav)}select:hover,select:focus{background:var(--foreground-hover);color:var(--color-nav)}select::picker-icon{content:"⌵";font-size:.8rem}select:open::picker-icon{rotate:180deg;transition:.3s}option{display:flex;justify-content:flex-start;padding:var(--padding_d2);background:var(--foreground);border:1px solid var(--border);gap:var(--padding_d2);transition:.6s;color:var(--color-nav)}option:first-of-type{border-radius:var(--radius)var(--radius)0 0}option:last-of-type{border-radius:0 0 var(--radius)var(--radius)}option:not(option:last-of-type){border-bottom:none}option:nth-of-type(even){background:var(--background-grey)}option:hover,option:focus{background:var(--foreground-hover)}option .icon{text-box:trim-both cap alphabetic}selectedcontent .icon{display:none}option:checked{background:var(--background-grey-embed)}option::checkmark{order:1;margin-left:auto;content:"✅ "}::picker(select){border:none;border-radius:var(--radius);top:calc(anchor(bottom) + 1px);left:calc(-1*var(--padding));opacity:0;transition:all .2s allow-discrete}::picker(select):popover-open{opacity:1}@starting-style{::picker(select):popover-open { + opacity: 0; + } +}html{scroll-behavior:smooth;scroll-padding:2em}body{display:flex;flex-direction:column;height:100%;color:var(--color);background:var(--background);font-family:var(--font-sans)}#content-wrapper{display:flex;flex:1;flex-direction:column;min-height:100svh}#content-wrapper>header{border-bottom:1px solid var(--border)}#content-wrapper>header>a{display:inline-block;padding:var(--padding)}#content-wrapper>footer{display:flex;flex-direction:column;padding:var(--padding_x2)var(--padding)0}#content-wrapper>footer>div{display:flex;align-items:center;justify-content:center;gap:var(--padding_d2);text-align:center;flex-wrap:wrap}#content-wrapper>footer>div:first-child{white-space:initial;padding:var(--padding)}#content-wrapper>footer>div:first-child a{background:var(--foreground-highlight);color:var(--color-highlight);border-radius:var(--radius);cursor:pointer;padding:0 var(--padding_d2)}#content-wrapper>footer>div:nth-child(2){justify-content:flex-end;padding:var(--padding)0}#content-wrapper>nav{display:flex;justify-content:end}@media(min-width:768px){#content-wrapper>header{border-bottom:none}#content-wrapper>nav>select{position:absolute;top:calc(var(--padding) - 4px);right:var(--padding)}}main{display:flex;flex:1;flex-direction:column;align-items:center;justify-content:center}main>section{display:flex}main>section>div{display:flex;flex-direction:column}main>section>div>header{display:flex;flex-direction:column;text-align:center;padding:var(--padding_x2)var(--padding);cursor:url("data:image/svg+xml;utf8, 🦄"),auto}main>section>div>header>h1{display:flex;font-size:2.5em;font-weight:800;padding-bottom:calc(var(--padding)*1.2);gap:var(--padding);flex-wrap:wrap;justify-content:center}main>section>div>header>h1>span{color:#866ee7}main>section>div>header>p{color:var(--color-fade);font-size:1.2em}main>section>div>div{display:flex;justify-content:center;gap:var(--padding_x2)}main>section>div>div>.btn{font-size:1.1em;padding:.4em 1em}main>section>div>div>.btn:first-child,main>section>div>div>.btn:first-child:hover{background:var(--foreground-highlight);color:var(--color-highlight)}main>section>div>div>.btn:last-child,main>section>div>div>.btn:last-child:hover{background:var(--foreground-hover)}@media(min-width:1024px){main>section>div>header{padding:var(--padding_x2)var(--padding_x2)calc(var(--padding)*3)}main>section>div>header>h1{font-size:3em}main>section>div>header>p{font-size:1.4em}main>section>div>div{gap:calc(var(--padding)*2.5)}main>section>div>div>.btn{font-size:1.27em}}@media(min-width:1280px){main>section>div>header{padding:var(--padding_x2)calc(var(--padding)*3)calc(var(--padding)*6)}main>section>div>header>h1{font-size:4em}main>section>div>header>p{font-size:1.6em}main>section>div>div{gap:calc(var(--padding)*2.6)}main>section>div>div>.btn{font-size:1.5em}} \ No newline at end of file diff --git a/docs/assets/js/docs.min.bb7187d7b0d361897c27b4f61653c7152f6c633ccc12981b2a0eca4a986cc54d.js b/docs/assets/js/docs.min.bb7187d7b0d361897c27b4f61653c7152f6c633ccc12981b2a0eca4a986cc54d.js new file mode 100644 index 00000000..5c95ff4b --- /dev/null +++ b/docs/assets/js/docs.min.bb7187d7b0d361897c27b4f61653c7152f6c633ccc12981b2a0eca4a986cc54d.js @@ -0,0 +1 @@ +const lsKeyColorPreference="color-preference",getColorPreference=()=>{let e=localStorage.getItem(lsKeyColorPreference);return e!==null?e:window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"};let colorPreference=getColorPreference();localStorage.setItem(lsKeyColorPreference,colorPreference),document.firstElementChild.setAttribute("data-color",colorPreference);const colorPreferenceButton=document.querySelector("#content-wrapper > footer > div:last-child > button:last-child");colorPreferenceButton&&colorPreferenceButton.addEventListener("click",function(){colorPreference=="dark"?colorPreference="light":colorPreference="dark",setColorPreference()});const setColorPreference=()=>{localStorage.setItem(lsKeyColorPreference,colorPreference),document.firstElementChild.setAttribute("data-color",colorPreference)};window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",({matches:e})=>{colorPreference=e?"dark":"light",setColorPreference()});const body=document.body,bodyModelOuter=document.querySelector("#body-model-outer"),asideWrapper=document.querySelector("#aside-wrapper"),asideWrapperOpenButton=document.querySelector("main > article > nav > button:first-child"),asideWrapperCloseButton=document.querySelector("#aside-wrapper > aside > div .btn"),asideWrapperSiteLogo=document.querySelector("#aside-wrapper .site-logo"),asideWrapperAsideNav=document.querySelector("#aside-wrapper > aside > nav"),asideWrapperAsideNavActiveItem=document.querySelector("#aside-wrapper > aside > nav > details > ul > li > a.active");asideWrapper&&asideWrapperOpenButton&&asideWrapperOpenButton.addEventListener("click",function(){body.classList.add("model-open"),bodyModelOuter.style.display="block",asideWrapper.classList.add("open"),asideWrapperAsideNavActiveItem.scrollIntoView({behavior:"auto",block:"center"}),asideWrapperCloseButton.addEventListener("click",function(){body.classList.remove("model-open"),bodyModelOuter.style.display="none",asideWrapper.classList.remove("open")}),bodyModelOuter.addEventListener("click",function(){body.classList.remove("model-open"),bodyModelOuter.style.display="none",asideWrapper.classList.remove("open")})});const mainAside=document.querySelector("main > aside"),mainAsideOpenButton=document.querySelector("main > article > nav > button:last-child"),mainAsideCloseButton=document.querySelector("main > aside > div > .btn");mainAside&&mainAsideOpenButton&&mainAsideOpenButton.addEventListener("click",function(){body.classList.add("model-open"),bodyModelOuter.style.display="block",mainAside.classList.add("open"),mainAsideCloseButton.addEventListener("click",function(){body.classList.remove("model-open"),bodyModelOuter.style.display="none",mainAside.classList.remove("open")}),bodyModelOuter.addEventListener("click",function(){body.classList.remove("model-open"),bodyModelOuter.style.display="none",mainAside.classList.remove("open")})}),window.addEventListener("resize",function(){body.classList.contains("model-open")&&(body.classList.remove("model-open"),bodyModelOuter.style.display="none",mainAside.classList.contains("open")&&mainAside.classList.remove("open"),asideWrapper.classList.contains("open")&&asideWrapper.classList.remove("open"))});const adjustAsideWrapperAsideNavHeight=function(){window.innerWidth>1280?asideWrapperAsideNav.style.height=`${window.innerHeight-1-asideWrapperSiteLogo.getBoundingClientRect().height}px`:asideWrapperAsideNav.style.height=""},scrollToActiveAsideNavItem=function(){const e=asideWrapperAsideNav?.querySelector("details > ul > li > a.active");e&&e.offsetTop>asideWrapperAsideNav.clientHeight&&asideWrapperAsideNav.offsetParent!==null&&asideWrapperAsideNav.scrollTo({top:e.offsetTop-asideWrapperAsideNav.offsetTop-asideWrapperSiteLogo.getBoundingClientRect().height,behavior:"auto"})};adjustAsideWrapperAsideNavHeight(),scrollToActiveAsideNavItem(),window.addEventListener("resize",function(){adjustAsideWrapperAsideNavHeight(),scrollToActiveAsideNavItem()}),"IntersectionObserver"in window&&document.addEventListener("DOMContentLoaded",function(){const n=document.querySelectorAll("#TableOfContents a");let e=null;const t={},s=new IntersectionObserver(n=>{n.forEach(n=>{n.isIntersecting&&(e&&e.classList.remove("active"),e=t[n.target.id],e&&e.classList.add("active"))})},{rootMargin:`0% 0% -80% 0%`});n.forEach(e=>{const n=e.getAttribute("href")?e.getAttribute("href").slice(1):null;if(n){const o=document.getElementById(n);o&&(t[n]=e,s.observe(o))}e.addEventListener("click",function(){body.classList.contains("model-open")&&mainAsideCloseButton.click()})})}) \ No newline at end of file diff --git a/docs/assets/js/home.min.c8547379013b8a9d3b0bb417291ab4d778359264b06feb44401e3a1a40062184.js b/docs/assets/js/home.min.c8547379013b8a9d3b0bb417291ab4d778359264b06feb44401e3a1a40062184.js new file mode 100644 index 00000000..fc74dea6 --- /dev/null +++ b/docs/assets/js/home.min.c8547379013b8a9d3b0bb417291ab4d778359264b06feb44401e3a1a40062184.js @@ -0,0 +1 @@ +const lsKeyColorPreference="color-preference",getColorPreference=()=>{let e=localStorage.getItem(lsKeyColorPreference);return e!==null?e:window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"};let colorPreference=getColorPreference();localStorage.setItem(lsKeyColorPreference,colorPreference),document.firstElementChild.setAttribute("data-color",colorPreference);const colorPreferenceButton=document.querySelector("#content-wrapper > footer > div:last-child > button:last-child");colorPreferenceButton&&colorPreferenceButton.addEventListener("click",function(){colorPreference=="dark"?colorPreference="light":colorPreference="dark",setColorPreference()});const setColorPreference=()=>{localStorage.setItem(lsKeyColorPreference,colorPreference),document.firstElementChild.setAttribute("data-color",colorPreference)};window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",({matches:e})=>{colorPreference=e?"dark":"light",setColorPreference()}) \ No newline at end of file diff --git a/docs/categories/index.xml b/docs/categories/index.xml new file mode 100644 index 00000000..c59a04b1 --- /dev/null +++ b/docs/categories/index.xml @@ -0,0 +1 @@ +Categories on Learning Rusthttps://learning-rust.github.io/categories/Recent content in Categories on Learning RustHugoen-US \ No newline at end of file diff --git a/docs/docs/borrowing/index.html b/docs/docs/borrowing/index.html new file mode 100644 index 00000000..a760e323 --- /dev/null +++ b/docs/docs/borrowing/index.html @@ -0,0 +1,79 @@ +Borrowing · Learning Rust

Borrowing

In real life applications, most of the times we have to pass variable bindings to other functions or assign them to other variable bindings. In this case, we are referencing the original binding; borrow the data of it.

What is Borrowing?

Borrow (verb)
To receive something with the promise of returning it.

Shared & Mutable borrowings

⭐️ There are two types of Borrowing,

  1. Shared Borrowing (&T)

    • A piece of data can be borrowed by a single or multiple users, but data should not be altered.
  2. Mutable Borrowing (&mut T)

    • A piece of data can be borrowed and altered by a single user, but the data should not be accessible for any other users at that time.

Rules for borrowings

There are very important rules regarding borrowing,

  1. One piece of data can be borrowed either as a shared borrow or as a mutable borrow at a given time. But not both at the same time.

  2. Borrowing applies for both copy types and move types.

  3. The concept of Liveness

fn main() {
+  let mut a = vec![1, 2, 3];
+  let b = &mut a;  //  &mut borrow of `a` starts here
+                   //  ⁝
+  // some code     //  ⁝
+  // some code     //  ⁝
+}                  //  &mut borrow of `a` ends here
+
+
+fn main() {
+  let mut a = vec![1, 2, 3];
+  let b = &mut a;  //  &mut borrow of `a` starts here
+  // some code
+
+  println!("{:?}", a); // trying to access `a` as a shared borrow, so giving an error
+}                  //  &mut borrow of `a` ends here
+
+
+fn main() {
+  let mut a = vec![1, 2, 3];
+  {
+    let b = &mut a;  //  &mut borrow of `a` starts here
+    // any other code
+  }                  //  &mut borrow of `a` ends here
+
+  println!("{:?}", a); // allow borrowing `a` as a shared borrow
+}
+

💡 Let’s see how to use shared and mutable borrowings in examples.

Examples for Shared Borrowing

fn main() {
+    let a = [1, 2, 3];
+    let b = &a;
+    println!("{:?} {}", a, b[0]); // [1, 2, 3] 1
+}
+
+
+fn main() {
+    let a = vec![1, 2, 3];
+    let b = get_first_element(&a);
+
+    println!("{:?} {}", a, b); // [1, 2, 3] 1
+}
+
+fn get_first_element(a: &Vec<i32>) -> i32 {
+    a[0]
+}
+

Examples for Mutable Borrowing

fn main() {
+    let mut a = [1, 2, 3];
+    let b = &mut a;
+    b[0] = 4;
+    println!("{:?}", b); // [4, 2, 3]
+}
+
+
+fn main() {
+    let mut a = [1, 2, 3];
+    {
+        let b = &mut a;
+        b[0] = 4;
+    }
+
+    println!("{:?}", a); // [4, 2, 3]
+}
+
+
+fn main() {
+    let mut a = vec![1, 2, 3];
+    let b = change_and_get_first_element(&mut a);
+
+    println!("{:?} {}", a, b); // [4, 2, 3] 4
+}
+
+fn change_and_get_first_element(a: &mut Vec<i32>) -> i32 {
+    a[0] = 4;
+    a[0]
+}
+
\ No newline at end of file diff --git a/docs/docs/borrowing/og.jpg b/docs/docs/borrowing/og.jpg new file mode 100644 index 00000000..7616cba3 Binary files /dev/null and b/docs/docs/borrowing/og.jpg differ diff --git a/docs/docs/cargo-crates-and-basic-project-structure/index.html b/docs/docs/cargo-crates-and-basic-project-structure/index.html new file mode 100644 index 00000000..056bc9b1 --- /dev/null +++ b/docs/docs/cargo-crates-and-basic-project-structure/index.html @@ -0,0 +1,44 @@ +Cargo, Crates and Basic Project Structure · Learning Rust

Cargo, Crates and Basic Project Structure

Cargo

Cargo is Rust’s built-in package manager and build system. It also supports the following actions,

CommandAction
cargo newCreate a new project
cargo initCreate a new project in an existing directory
cargo checkVerify the project compiles without errors
cargo buildBuild the executable
cargo runBuild the executable and run
cargo cleanRemove the build system directories/ target directory

💡 The cargo check command verifies that the project compiles without errors, without producing an executable. +Thus, it is often faster than cargo build.

💡 Cargo places executables compiled with cargo build or cargo run in the target/debug/ directory. +But, while those built with cargo build --release for release purposes are stored in target/release/ directory. +Release builds use more optimizations and remove some runtime safety checks to increase performance, although this comes at the cost of longer compile time.

CommandAction
cargo addAdd a dependency crate to the project
cargo removeRemove a dependency crate from the project
cargo fetchDownload the dependencies specified in Cargo.lock
cargo updateUpdate project dependencies

💡 A crate is a package that can be shared via crates.io, Rust community’s crate registry. +cargo add, cargo remove, cargo fetch, and cargo update commands manage project dependencies through the crate hosted on crates.io.

💡 The cargo add command includes a specified crate in the [dependencies] section of Cargo.toml, while cargo add --dev adds a crate to the [dev-dependencies] section. This indicates that the crate is only used for development purposes like testing and will not be included in the final compiled code.

CommandAction
cargo testRun tests
cargo benchRun benchmarks
cargo docGenerate the project documentation via rustdoc

In addition, there are cargo commands to publish the project as a crate to crates.io.

CommandAction
cargo loginLogin to crates.io with the API token
cargo packageMake the local crate uploadable to crates.io
cargo publishUpload the crate to crates.io
cargo installInstall a Rust binary
cargo uninstallUninstall a Rust binary

💡 You need to get an API token from crates.io to publish a crate to it. The API token can be found in the Account Settings page, after login to that site. We will discuss more about this under code organization with crates.

Crate

  • A crate is a package, which can be shared via Rust community’s crate registry, crates.io.

  • A crate can produce an executable or a library. In other words, it can be a binary crate or a library crate.

    1. cargo new crate_name --bin or cargo new crate_name: Produces an executable
    2. cargo new crate_name --lib: Produces a library

The first one generates,

├── Cargo.toml
+└── src
+    └── main.rs
+

and the second one generates,

├── Cargo.toml
+└── src
+    └── lib.rs
+
  • Cargo.toml(capital c) is the configuration file which contains all of the metadata that Cargo needs to compile your project.
  • src folder is the place to store the source code.
  • Each crate has an implicit crate root/ entry point. main.rs is the crate root for a binary crate and lib.rs is the crate root for a library crate.

Project Structure

This is how Cargo documentation describes about the recommended project layout,

.
+├── Cargo.toml
+├── Cargo.lock
+├── src
+│   ├── main.rs
+│   ├── lib.rs
+│   └── bin
+│       ├── another_executable.rs
+│       └── multi_file_executable
+│           ├── main.rs
+│           └── some_module.rs
+├── tests
+│   └── some_integration_tests.rs
+├── benches
+│   └── simple_bench.rs
+└── examples
+    └── simple_example.rs
+
  • The source code goes in the src directory.
    • The default executable file is src/main.rs.
    • The default library file is src/lib.rs.
    • Other executables can be placed in,
      • src/bin/*.rs
      • src/bin/*/main.rs
  • Integration tests go in the tests directory (unit tests go in each file they’re testing).
  • Benchmarks go in the benches directory.
  • Examples go in the examples directory.

Rust Editions

Rust guarantees backward compatibility while introducing major updates to the language. To support this, the edition field was added to the Cargo.toml file in Rust 2018, marking the first major update to the language ecosystem three years after its initial release. Editions are opt-in, meaning existing crates will not experience these changes until they explicitly migrate to the new edition.

The major editions of Rust are:

  • Rust 2015: The initial edition, introduced with Rust 1.0. It established the core language features like ownership, borrowing, and lifetimes, laying the foundation for Rust’s safety and concurrency guarantees.

  • Rust 2018: The first major update, introduced the edition field in Cargo.toml, simplified the module system, stabilized async/await, improved error handling with the ? operator, and made several syntactic changes.

  • Rust 2021: Focused on improving ergonomics and removing inconsistencies, such as disjoint closure capture, IntoIterator for arrays, and the introduction of or-patterns in macros.

  • Rust 2024: The latest edition, includes enhancements like refined async features, more const generics, better diagnostics, and improved Cargo features.

For new projects created by cargo new, it will set edition = "2024" by default in the Cargo.toml file. For example,

[package]
+name = "hello_world"
+version = "0.1.0"
+edition = "2024"
+

👨‍🏫 Before going to the next…

  • The .cargo/bin directory of your home directory is the default location of Rust binaries. Not only the official binaries like rustc, cargo, rustup, rustfmt, rustdoc, rust-analyzer and also the binaries you can install via cargo install command, will be stored in this directory.

  • Even though the initial convention for naming crates and file names is using the snake_case, some crate developers are using kebab-case on both crates and file names. To make your code more consistent, use the initial convention snake_case; especially on file names.

  • Create,

    1. an executable crate via cargo new command, run it via cargo run and examine the files and project structure.

    2. a library crate via cargo new command, run cargo test and examine the files and project structure.

    3. a multiple executables project and try to run each executable.

      • You can name executables in the Cargo.toml file.
        [[bin]]
        +name = "app"
        +path = "src/bin/app/main.rs"
        +
      • You can use the --bin flag to specify the executable, while running cargo commands.
        Ex. cargo build --bin app, cargo run --bin app
      • You can set default executable in the Cargo.toml file.
        [package]
        +name = "hello_world"
        +version = "0.1.0"
        +edition = "2024"
        +default-run = "app"
        +
    4. Run cargo build --release and check the files in the target folder.

\ No newline at end of file diff --git a/docs/docs/cargo-crates-and-basic-project-structure/og.jpg b/docs/docs/cargo-crates-and-basic-project-structure/og.jpg new file mode 100644 index 00000000..9d05f064 Binary files /dev/null and b/docs/docs/cargo-crates-and-basic-project-structure/og.jpg differ diff --git a/docs/docs/combinators/index.html b/docs/docs/combinators/index.html new file mode 100644 index 00000000..49ac7470 --- /dev/null +++ b/docs/docs/combinators/index.html @@ -0,0 +1,171 @@ +Combinators · Learning Rust

Combinators

What is a combinator?

  • One meaning of “combinator” is a more informal sense referring to the combinator pattern, a style of organizing libraries centered around the idea of combining things. Usually there is some type T, some functions for constructing “primitive” values of type T, and some “combinators” which can combine values of type T in various ways to build up more complex values of type T. The other definition is “function with no free variables”. +__ wiki.haskell.org

  • A combinator is a function which builds program fragments from program fragments; in a sense the programmer using combinators constructs much of the desired program automatically, rather that writing every detail by hand. +__ John Hughes—Generalizing Monads to Arrows via Functional Programming Concepts

The exact definition of “combinators” in Rust ecosystem is bit unclear. 

  • or(), and(), or_else(), and_then()

    • Combine two values of type T and return same type T.
  • xor() for Option types

    • Combine two values of type T and return same type T, only if exactly one value is T
  • filter() for Option types

    • Filter type T by using a closure as a conditional function
    • Return same type T
  • map(), map_err()

    • Convert type T by applying a closure.
    • The data type of the value inside T can be changed. +ex. Some<&str> can be converted to Some<usize> or Err<&str> to Err<isize> and etc.
  • map_or(), map_or_else()

    • Transform type T by applying a closure & return the value inside type T.
    • For None and Err, a default value or another closure is applied.
  • ok_or(), ok_or_else() for Option types

    • Transform Option type into a Result type.
  • as_ref(), as_mut()

    • Transform type T into a reference or a mutable reference.

or() and and()

While combining two expressions, which return either Option/ Result

  • or(): If either one got Some or Ok, that value returns immediately.
  • and(): If both got Some or Ok, the value in the second expression returns. If either one got None or Err that value returns immediately.

With Option

let s1 = Some("some1");
+let s2 = Some("some2");
+let n: Option<&str> = None;
+
+assert_eq!(s1.or(s2), s1); // Some1 or Some2 = Some1
+assert_eq!(s1.or(n), s1);  // Some or None = Some
+assert_eq!(n.or(s1), s1);  // None or Some = Some
+assert_eq!(n.or(n), n);    // None1 or None2 = None2
+
+assert_eq!(s1.and(s2), s2); // Some1 and Some2 = Some2
+assert_eq!(s1.and(n), n);   // Some and None = None
+assert_eq!(n.and(s1), n);   // None and Some = None
+assert_eq!(n.and(n), n);    // None1 and None2 = None1
+

With Result

let o1: Result<&str, &str> = Ok("ok1");
+let o2: Result<&str, &str> = Ok("ok2");
+let e1: Result<&str, &str> = Err("error1");
+let e2: Result<&str, &str> = Err("error2");
+
+assert_eq!(o1.or(o2), o1); // Ok1 or Ok2 = Ok1
+assert_eq!(o1.or(e1), o1); // Ok or Err = Ok
+assert_eq!(e1.or(o1), o1); // Err or Ok = Ok
+assert_eq!(e1.or(e2), e2); // Err1 or Err2 = Err2
+
+assert_eq!(o1.and(o2), o2); // Ok1 and Ok2 = Ok2
+assert_eq!(o1.and(e1), e1); // Ok and Err = Err
+assert_eq!(e1.and(o1), e1); // Err and Ok = Err
+assert_eq!(e1.and(e2), e1); // Err1 and Err2 = Err1
+

xor()

While combining two Options, which return either Option, only if exactly one option is T.

The same Some type is returned, only if we pass only one Some value. None is returned, if both Some or None type. Rust support xor() only for Option types.

let s1 = Some("some1");
+let s2 = Some("some2");
+let n: Option<&str> = None;
+
+assert_eq!(s1.xor(s2), n); // Some1 xor Some2 = None
+assert_eq!(s1.xor(n), s1); // Some xor None = Some
+assert_eq!(n.xor(s1), s1); // None xor Some = Some
+assert_eq!(n.xor(n), n); // None1 xor None2 = None2
+

or_else()

Similar to or(). The only difference is, the second expression should be a closure which returns same type T.

With Option

let s1 = Some("some1");
+let s2 = Some("some2");
+let fn_some = || Some("some2"); // similar to: let fn_some = || -> Option<&str> { Some("some2") };
+
+let n: Option<&str> = None;
+let fn_none = || None;
+
+assert_eq!(s1.or_else(fn_some), s1);  // Some1 or_else Some2 = Some1
+assert_eq!(s1.or_else(fn_none), s1);  // Some or_else None = Some
+assert_eq!(n.or_else(fn_some), s2);   // None or_else Some = Some
+assert_eq!(n.or_else(fn_none), None); // None1 or_else None2 = None2
+

With Result

let o1: Result<&str, &str> = Ok("ok1");
+let o2: Result<&str, &str> = Ok("ok2");
+let fn_ok = |_| Ok("ok2"); // similar to: let fn_ok = |_| -> Result<&str, &str> { Ok("ok2") };
+
+let e1: Result<&str, &str> = Err("error1");
+let e2: Result<&str, &str> = Err("error2");
+let fn_err = |_| Err("error2");
+
+assert_eq!(o1.or_else(fn_ok), o1);  // Ok1 or_else Ok2 = Ok1
+assert_eq!(o1.or_else(fn_err), o1); // Ok or_else Err = Ok
+assert_eq!(e1.or_else(fn_ok), o2);  // Err or_else Ok = Ok
+assert_eq!(e1.or_else(fn_err), e2); // Err1 or_else Err2 = Err2
+

and_then()

Similar to and(). The only difference is, the second expression should be a closure which returns same type T.

With Option

let s1 = Some("some1");
+let s2 = Some("some2");
+let fn_some = |_| Some("some2"); // similar to: let fn_some = |_| -> Option<&str> { Some("some2") };
+
+let n: Option<&str> = None;
+let fn_none = |_| None;
+
+assert_eq!(s1.and_then(fn_some), s2); // Some1 and_then Some2 = Some2
+assert_eq!(s1.and_then(fn_none), n);  // Some and_then None = None
+assert_eq!(n.and_then(fn_some), n);   // None and_then Some = None
+assert_eq!(n.and_then(fn_none), n);   // None1 and_then None2 = None1
+

With Result

let o1: Result<&str, &str> = Ok("ok1");
+let o2: Result<&str, &str> = Ok("ok2");
+let fn_ok = |_| Ok("ok2"); // similar to: let fn_ok = |_| -> Result<&str, &str> { Ok("ok2") };
+
+let e1: Result<&str, &str> = Err("error1");
+let e2: Result<&str, &str> = Err("error2");
+let fn_err = |_| Err("error2");
+
+assert_eq!(o1.and_then(fn_ok), o2);  // Ok1 and_then Ok2 = Ok2
+assert_eq!(o1.and_then(fn_err), e2); // Ok and_then Err = Err
+assert_eq!(e1.and_then(fn_ok), e1);  // Err and_then Ok = Err
+assert_eq!(e1.and_then(fn_err), e1); // Err1 and_then Err2 = Err1
+

filter()

💡 Usually in programming languages filter functions are used with arrays or iterators to create a new array/ iterator by filtering own elements via a function/ closure. Rust also provides filter() as an iterator adaptor to apply a closure on each element of an iterator to transform it into another iterator. However in here we are talking about the functionality of filter() with Option types.

The same Some type is returned, only if we pass a Some value and the given closure returned true for it. None is returned, if None type passed or the closure returned false. The closure uses the value inside Some as an argument. Still Rust support filter() only for Option types.

let s1 = Some(3);
+let s2 = Some(6);
+let n = None;
+
+let fn_is_even = |x: &i8| x % 2 == 0;
+
+assert_eq!(s1.filter(fn_is_even), n);  // Some(3) -> 3 is not even -> None
+assert_eq!(s2.filter(fn_is_even), s2); // Some(6) -> 6 is even -> Some(6)
+assert_eq!(n.filter(fn_is_even), n);   // None -> no value -> None
+

map() and map_err()

💡 Usually in programming languages map() functions are used with arrays or iterators, to apply a closure on each element of the array or iterator. Rust also provides map() as an iterator adaptor to apply a closure on each element of an iterator to transform it into another iterator. However in here we are talking about the functionality of map() with Option and Result types.

  • map() : Convert type T by applying a closure. The data type of Some or Ok blocks can be changed according to the return type of the closure. Convert Option<T> to Option<U>, Result<T, E> to Result<U, E>

⭐ Via map(), only Some and Ok values are getting changed. No affect to the values inside Err (None doesn’t contain any value at all).

With Option

let s1 = Some("abcde");
+let s2 = Some(5);
+
+let n1: Option<&str> = None;
+let n2: Option<usize> = None;
+
+let fn_character_count = |s: &str| s.chars().count();
+
+assert_eq!(s1.map(fn_character_count), s2); // Some1 map = Some2
+assert_eq!(n1.map(fn_character_count), n2); // None1 map = None2
+

With Result

let o1: Result<&str, &str> = Ok("abcde");
+let o2: Result<usize, &str> = Ok(5);
+
+let e1: Result<&str, &str> = Err("abcde");
+let e2: Result<usize, &str> = Err("abcde");
+
+let fn_character_count = |s: &str| s.chars().count();
+
+assert_eq!(o1.map(fn_character_count), o2); // Ok1 map = Ok2
+assert_eq!(e1.map(fn_character_count), e2); // Err1 map = Err2
+
  • map_err() for Result types : The data type of Err blocks can be changed according to the return type of the closure. Convert Result<T, E> to Result<T, F>.

⭐ Via map_err(), only Err values are getting changed. No affect to the values inside Ok.

let o1: Result<&str, &str> = Ok("abcde");
+let o2: Result<&str, isize> = Ok("abcde");
+
+let e1: Result<&str, &str> = Err("404");
+let e2: Result<&str, isize> = Err(404);
+
+let fn_character_count = |s: &str| -> isize { s.parse().unwrap() }; // convert str to isize
+
+assert_eq!(o1.map_err(fn_character_count), o2); // Ok1 map = Ok2
+assert_eq!(e1.map_err(fn_character_count), e2); // Err1 map = Err2
+

map_or() and map_or_else()

Hope you remember the functionality of unwrap_or() and unwrap_or_else() functions. These functions also bit similar to them. But map_or() and map_or_else() apply a closure on Some and Ok values and return the value inside type T.

  • map_or() : Support only for Option types (not supporting Result). Apply the closure to the value inside Some and return the output according to the closure. The given default value is returned for None types.
const V_DEFAULT: i8 = 1;
+
+let s = Some(10);
+let n: Option<i8> = None;
+let fn_closure = |v: i8| v + 2;
+
+assert_eq!(s.map_or(V_DEFAULT, fn_closure), 12);
+assert_eq!(n.map_or(V_DEFAULT, fn_closure), V_DEFAULT);
+
  • map_or_else() : Support for both Option and Result types. Similar to map_or() but should provide another closure instead a default value for the first parameter.

None types doesn’t contain any value. So no need to pass anything to the closure as input with Option types. But Err types contain some value inside it. So default closure should able to read it as an input, while using this with Result types.

let s = Some(10);
+let n: Option<i8> = None;
+
+let fn_closure = |v: i8| v + 2;
+let fn_default = || 1; // None doesn't contain any value. So no need to pass anything to closure as input.
+
+assert_eq!(s.map_or_else(fn_default, fn_closure), 12);
+assert_eq!(n.map_or_else(fn_default, fn_closure), 1);
+
let o = Ok(10);
+let e = Err(5);
+
+let fn_closure = |v: i8| v + 2;
+let fn_default_for_result = |v: i8| v + 1; // Err contain some value inside it. So default closure should able to read it as input
+
+assert_eq!(o.map_or_else(fn_default_for_result, fn_closure), 12);
+assert_eq!(e.map_or_else(fn_default_for_result, fn_closure), 6);
+

ok_or() and ok_or_else()

As mentioned earlier, ok_or(), ok_or_else() transform Option type into Result type. Some to Ok and None to Err.

  • ok_or() : A default Err message should pass as argument.
const ERR_DEFAULT: &str = "error message";
+
+let s = Some("abcde");
+let n: Option<&str> = None;
+
+let o: Result<&str, &str> = Ok("abcde");
+let e: Result<&str, &str> = Err(ERR_DEFAULT);
+
+assert_eq!(s.ok_or(ERR_DEFAULT), o); // Some(T) -> Ok(T)
+assert_eq!(n.ok_or(ERR_DEFAULT), e); // None -> Err(default)
+
  • ok_or_else() : Similar to ok_or(). A closure should be passed as the argument.
let s = Some("abcde");
+let n: Option<&str> = None;
+let fn_err_message = || "error message";
+
+let o: Result<&str, &str> = Ok("abcde");
+let e: Result<&str, &str> = Err("error message");
+
+assert_eq!(s.ok_or_else(fn_err_message), o); // Some(T) -> Ok(T)
+assert_eq!(n.ok_or_else(fn_err_message), e); // None -> Err(default)
+

as_ref() and as_mut()

🔎 As mentioned earlier, these functions are used to borrow type T as a reference or as a mutable reference.

  • as_ref() : Convert Option<T> to Option<&T> and Result<T, E> to Result<&T, &E>
  • as_mut() : Converts Option<T> to Option<&mut T> and Result<T, E> to Result<&mut T, &mut E>
\ No newline at end of file diff --git a/docs/docs/combinators/og.jpg b/docs/docs/combinators/og.jpg new file mode 100644 index 00000000..b6be4af0 Binary files /dev/null and b/docs/docs/combinators/og.jpg differ diff --git a/docs/docs/comments-and-documenting-the-code/index.html b/docs/docs/comments-and-documenting-the-code/index.html new file mode 100644 index 00000000..54769e2a --- /dev/null +++ b/docs/docs/comments-and-documenting-the-code/index.html @@ -0,0 +1,42 @@ +Comments and Documenting the code · Learning Rust

Comments and Documenting the code

Comments

// Line comments
+/* Block comments */
+

Nested block comments are supported.

💡 By convention, try to avoid using block comments. Use line comments instead.

Doc Comments

As we discussed, we can generate the project documentation via rustdoc by running the cargo doc command. It uses the doc comments to generate the documentation.

💡 Usually we are adding doc comments on library crates. Also, we can use Markdown notations inside the doc comments.

/// Line comments; document the next item
+/** Block comments; document the next item */
+
+//! Line comments; document the enclosing item
+/*! Block comments; document the enclosing item !*/
+

For example,

/// This module contains tests; Outer comment
+mod tests {
+
+}
+
+mod tests {
+    //! This module contains tests; Inner comment
+
+}
+

💭 The mod keyword is used for modules. Don’t worry about this for now; it’ll be discussed later.

Doc Attributes

Doc attributes are alternatives for doc comments. Especially, we use these doc attributes while we need to set controls on rustdoc. Refer the doc attributes section of rustdoc documentation for more details.

In the following example, each comment is equivalent to relevant doc attribute.

/// Outer comment
+#[doc = "Outer comment"]
+
+//! Inner comment
+#![doc = "Inner comment"]
+

🔎 An attribute is a general, free-form metadatum that is interpreted according to the name, convention, language and compiler version. Any item declaration may have an attribute applied to it. Syntax:

  • Outer attribute: #[attr]
  • Inner attribute: #![attr]

👨‍🏫 Before going to the next…

  • Use //! only to write crate-level documentation, nothing else. When using mod blocks, use /// outside of the block. Check the usage of //! and /// doc comments of few popular crates on crates.io. For example, check serde/src/lib.rs and rand/src/lib.rs.

  • Run cargo new hello_lib --lib command to create a sample crate and replace its src/lib.rs file with the following code. Then run cd hello_lib && cargo doc --open to generate the documentation and open it from your web browser.

    //! A Simple Hello World Crate
    +
    +/// This function returns the greeting; Hello, world!
    +pub fn hello() -> String {
    +    ("Hello, world!").to_string()
    +}
    +
    +#[cfg(test)]
    +mod tests {
    +    use super::hello;
    +
    +    #[test]
    +    fn test_hello() {
    +        assert_eq!(hello(), "Hello, world!");
    +    }
    +}
    +
\ No newline at end of file diff --git a/docs/docs/comments-and-documenting-the-code/og.jpg b/docs/docs/comments-and-documenting-the-code/og.jpg new file mode 100644 index 00000000..20658698 Binary files /dev/null and b/docs/docs/comments-and-documenting-the-code/og.jpg differ diff --git a/docs/docs/control-flows/index.html b/docs/docs/control-flows/index.html new file mode 100644 index 00000000..e583685a --- /dev/null +++ b/docs/docs/control-flows/index.html @@ -0,0 +1,229 @@ +Control Flows · Learning Rust

Control Flows

if - else if - else

if

let age = 13;
+
+if age < 18 {
+    println!("Hello, child!"); // The code prints this
+}
+

if else

let i = 7;
+
+if i % 2 == 0 {
+    println!("Even");
+} else {
+    println!("Odd"); // The code prints this
+}
+

With let Statements

let age: u8 = 13;
+let is_below_eighteen = if age < 18 { true } else { false }; // true
+

if else if else

i. A simple example,

let team_size = 7;
+
+if team_size < 5 {
+    println!("Small");
+} else if team_size < 10 {
+    println!("Medium"); // The code prints this
+} else {
+    println!("Large");
+}
+

ii. Let’s refactor the above code,

let team_size = 7;
+let team_size_in_text;
+
+if team_size < 5 {
+    team_size_in_text = "Small";
+} else if team_size < 10 {
+    team_size_in_text = "Medium";
+} else {
+    team_size_in_text = "Large";
+}
+
+println!("Current team size : {}", team_size_in_text); // Current team size : Medium
+

iii. Let’s refactor further (variable shadowing),

let team_size = 7;
+let team_size = if team_size < 5 {
+    "Small" // ⭐️ no ;
+} else if team_size < 10 {
+    "Medium"
+} else {
+    "Large"
+};
+
+println!("Current team size : {}", team_size); // Current team size : Medium
+

⭐️ Return data type should be the same on each block when using this as an expression.

match

With Multiple Patterns

let tshirt_width = 20;
+let tshirt_size = match tshirt_width {
+    13 => "XS",     // check 13
+    14 | 15 => "S", // check 14 and 15
+    16..18 => "M",  // check from 16 to 17 / 18 exclusive (16,17)
+    18..=20 => "L", // check from 18 to 20 (18,19,20)
+
+    x if x > 20 && x < 26 => "XL", // check 21 to 25 (21,22,23,24,25)
+    // >,>=,<,<= via assigning the value to a variable (x) + if
+
+    _ => "Not Available", // default behavior, if none of conditions matches
+};
+
+println!("{}", tshirt_size); // L
+

Without Default Behavior

let is_allowed = false;
+let list_type = match is_allowed {
+    true => "Full",
+    false => "Restricted"
+    // no default/ _ condition can be skipped
+    // Because data type of is_allowed is boolean and all possibilities checked on conditions
+};
+
+println!("{}", list_type); // Restricted
+

With Multiple Variable Matchings

let marks_paper_a: u8 = 25;
+let marks_paper_b: u8 = 30;
+
+let output = match (marks_paper_a, marks_paper_b) {
+    (50, 50) => "Full marks for both papers",
+    (50, _) => "Full marks for paper A",
+    (_, 50) => "Full marks for paper B",
+    (x, y) if x > 25 && y > 25 => "Good",
+    (_, _) => "Work hard"
+};
+
+println!("{}", output); // Work hard
+

loop

Infinite loop

// ⚠️ This will run forever without terminating. Termininate manually (Ctrl+C)
+loop {
+    println!("Loop forever!");
+}
+

With break and continue

let mut a = 0;
+
+loop {
+    if a == 0 {
+        println!("Skip Value : {}", a);
+        a += 1;
+        continue;
+    } else if a == 2 {
+        println!("Break At : {}", a);
+        break;
+    }
+
+    println!("Current Value : {}", a);
+    a += 1;
+}
+

Returning a Value with break

let (mut x, y) = (1, 10);
+
+let z = loop {
+    x *= 2; // x = 2,4,8,16...
+
+    if x >= y { // 16 >= 10, so return 16
+        break x;
+    }
+};
+
+println!("{z}"); // 16
+

Labeling and break the Outer loop

let mut b1 = 1;
+
+'outer: loop { // set label outer (or any other snake_case name)
+    let mut b2 = 1;
+
+    'inner: loop { // set label inner
+        println!("Current Value : [{}][{}]", b1, b2);
+
+        if b1 == 2 && b2 == 2 {
+            break 'outer; // kill outer loop
+        } else if b2 == 5 {
+            break;
+        }
+
+        b2 += 1;
+    }
+
+    b1 += 1;
+}
+

while

Infinite while

// ⚠️ This will run forever without terminating. Termininate manually (Ctrl+C)
+while true {
+    println!("While forever!");
+}
+

A simple while

let mut a = 1;
+
+while a <= 10 {
+    println!("Current value : {}", a);
+    a += 1; //no ++ or -- on Rust
+}
+

With break and continue

let mut b = 0;
+
+while b < 5 {
+    if b == 0 {
+        println!("Skip value : {}", b);
+        b += 1;
+        continue;
+    } else if b == 2 {
+        println!("Break At : {}", b);
+        break;
+    }
+
+    println!("Current value : {}", b);
+    b += 1;
+}
+
+// 💡 You can't break with a value in a while
+

Labeling and break the Outer while

let mut c1 = 1;
+
+'outer: while c1 < 6 { // set label outer (or any other snake_case name)
+    let mut c2 = 1;
+
+    'inner: while c2 < 6 { // set label inner
+        println!("Current Value : [{}][{}]", c1, c2);
+
+        if c1 == 2 && c2 == 2 {
+            break 'outer; // kill outer while
+        }
+
+        c2 += 1;
+    }
+
+    c1 += 1;
+}
+

for

A simple for

// 0 to 10 (10 exclusive); In other languages, `for(i = 0; i < 10; i++)`
+for i in 0..10 {
+    println!("Current value : {}", i);
+}
+
// 1 to 10 (10 inclusive); In other languages, `for(i = 1; i <= 10; i++)`
+for i in 1..=10 {
+    println!("Current value : {}", i);
+}
+

With break and continue

// 💡 You can't break with a value in a for
+for b in 0..6 {
+    if b == 0 {
+        println!("Skip Value : {}", b);
+        continue;
+    } else if b == 2 {
+        println!("Break At : {}", b);
+        break;
+    }
+
+    println!("Current value : {}", b);
+}
+

Labeling and break the Outer for

'outer: for c1 in 1..6 { // set label outer (or any other snake_case name)
+
+    'inner: for c2 in 1..6 { // set label inner
+        println!("Current Value : [{}][{}]", c1, c2);
+
+        if c1 == 2 && c2 == 2 {
+            break 'outer; // kill outer for
+        }
+    }
+}
+

With Arrays and Vectors

let group : [&str; 4] = ["Mark", "Larry", "Bill", "Steve"];
+
+// 👎 group.len() = 4 check on each iteration of the for loop
+for n in 0..group.len() {
+    println!("Current Person : {}", group[n]);
+}
+
+// 👍 group.iter() turn the array into a simple iterator
+for person in group.iter() {
+    println!("Current Person : {person}");
+}
+
+// 👍 group.iter().enumerate() helps to read both the current index (starting from zero) and the value
+for (index, person) in group.iter().enumerate() {
+    println!("Person {index} : {person}");
+}
+

With a Vector of Tuples

let list = vec![(1, "Mark"), (2, "Larry"), (3, "Steve")];
+
+for (index, person) in list {
+    println!("Person {index} : {person}");
+}
+
\ No newline at end of file diff --git a/docs/docs/control-flows/og.jpg b/docs/docs/control-flows/og.jpg new file mode 100644 index 00000000..aa79424c Binary files /dev/null and b/docs/docs/control-flows/og.jpg differ diff --git a/docs/docs/crates/index.html b/docs/docs/crates/index.html new file mode 100644 index 00000000..56fc758a --- /dev/null +++ b/docs/docs/crates/index.html @@ -0,0 +1,197 @@ +Crates · Learning Rust

Crates

💭 Crates are a bit similar to the packages in some other languages. Crates compile individually. If the crate has child file modules, those files will get merged with the crate file and compile as a single unit.

💭 A crate can produce an executable/ a binary or a library. src/main.rs is the crate root/ entry point for a binary crate and src/lib.rs is the entry point for a library crate.

01. lib.rs on executable crate

💡 When writing binary crates, we can move the main functionalities to src/lib.rs and use it as a library from src/main.rs. This pattern is quite common on executable crates.

// # Think we run,
+cargo new greetings
+touch greetings/src/lib.rs
+
+// # It generates,
+greetings
+ ├── Cargo.toml
+ └── src
+    ├── lib.rs
+    └── main.rs
+
+// # Think we modify following files,
+
+// 01. greetings/src/lib.rs
+pub fn hello() {
+    println!("Hello, world!");
+}
+
+// 02. greetings/src/main.rs
+fn main() {
+    greetings::hello();
+}
+

💯 As I mentioned earlier, in here we use simplest examples to reduce the complexity of learning materials. But this is how we need to write greetings/src/lib.rs to make the code more testable.

// greetings/src/lib.rs
+pub fn hello() -> String {
+  //! This returns `Hello, world!` String
+  ("Hello, world!").to_string()
+}
+
+// 01. Tests for `hello()`
+#[test] // Indicates that this is a test function
+fn test_hello() {
+  assert_eq!(hello(), "Hello, world!");
+}
+
+// 02. Tests for `hello()`, Idiomatic way
+#[cfg(test)] // Only compiles when running tests
+mod tests { // Separates tests from code
+  use super::hello; // Import root `hello()` function
+  
+    #[test]
+    fn test_hello() {
+        assert_eq!(hello(), "Hello, world!");
+    }
+}
+

📖 When importing a crate that has dashes in its name “like-this”, which is not a valid Rust identifier, it will be converted by changing the dashes to underscores, so you would write extern crate like_this;

lib.rs can link with multiple files.

// # Think we run,
+cargo new phrases
+touch phrases/src/lib.rs
+touch phrases/src/greetings.rs
+
+// # It generates,
+phrases
+ ├── Cargo.toml
+ └── src
+    ├── greetings.rs
+    ├── lib.rs
+    └── main.rs
+   
+// # Think we modify following files,
+
+// 01. phrases/src/greetings.rs
+pub fn hello() {
+    println!("Hello, world!");
+}
+
+// 02. phrases/src/main.rs
+fn main() {
+    phrases::greetings::hello();
+}
+
+// 03. phrases/src/lib.rs
+pub mod greetings; // ⭐️ Import `greetings` module as a public module
+

02. Dependency crate on Cargo.toml

When the code in the lib.rs file is getting larger, we can move those into a separate library crate and use it as a dependency of the main crate. As we mentioned earlier, a dependency can be specified from a folder path, git repository or by crates.io.

a. Using folder path

Let’s see how to create a nested crate and use it as a dependency using folder path,

// # Think we run,
+cargo new phrases
+cargo new phrases/greetings --lib
+
+// # It generates,
+phrases
+ ├── Cargo.toml
+ ├── greetings
+   ├── Cargo.toml
+   └── src
+      └── lib.rs
+ └── src
+    └── main.rs
+
+// # Think we modify following files,
+
+// 01. phrases/Cargo.toml
+[package]
+name = "phrases"
+version = "0.1.0"
+authors = ["Dumindu Madunuwan"]
+
+[dependencies]
+greetings = { path = "greetings" }
+
+// 02. phrases/greetings/src/lib.rs
+pub fn hello() {
+    println!("Hello, world!");
+}
+
+// 03. phrases/src/main.rs
+extern crate greetings;
+
+fn main() {
+    greetings::hello();
+}
+

b. Using git repository

If you want to use a library crate on multiple projects, one way is moving crate code to a git repository and use it as a dependency when needed.

// -- Cargo.toml --
+[dependencies]
+
+// 01. Get the latest commit on the master branch
+rocket = { git = "https://github.com/SergioBenitez/Rocket" }
+
+// 02. Get the latest commit of a specific branch
+rocket = { git = "https://github.com/SergioBenitez/Rocket", branch = "v0.3" }
+
+// 03. Get a specific tag
+rocket = { git = "https://github.com/SergioBenitez/Rocket", tag = "v0.3.2" }
+
+// 04. Get a specific revision (on master or any branch, according to rev)
+rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "8183f636305cef4adaa9525506c33cbea72d1745" }
+

c. Using crates.io

The other way is uploading it to crates.io and use it as a dependency when needed.

🚧 First, let’s create a simple “Hello world” crate and upload it to crates.io.

// # Think we run,
+cargo new test_crate_hello_world --lib
+
+// # It generates,
+test_crate_hello_world
+ ├── Cargo.toml
+ └── src
+    └── lib.rs
+   
+// # Think we modify following files,
+
+// 01. test_crate_hello_world/Cargo.toml
+[package]
+name = "test_crate_hello_world"
+version = "0.1.0"
+authors = ["Dumindu Madunuwan"]
+
+description = "A Simple Hello World Crate"
+repository = "https://github.com/dumindu/test_crate_hello_world"
+keywords = ["hello", "world"]
+license = "Apache-2.0"
+
+[dependencies]
+
+// 02. test_crate_hello_world/src/lib.rs
+//! A Simple Hello World Crate
+
+/// This function returns the greeting; `Hello, world!`
+pub fn hello() -> String {
+    ("Hello, world!").to_string()
+}
+
+#[cfg(test)]
+mod tests {
+
+    use super::hello;
+    
+    #[test]
+    fn test_hello() {
+        assert_eq!(hello(), "Hello, world!");
+    }
+}
+

💭 //! doc comments are used to write crate and module-level documentation. On other places, we have to use /// outside of the block. And when uploading a crate to crates.io, cargo generates the documentation from these doc comments and host it on docs.rs.

💡 We have to add the description and license fields to Cargo.toml. Otherwise, we will get error: api errors: missing or empty metadata fields: description, license. Please see http://doc.crates.io/manifest.html

To upload this to crates.io,

  1. We have to create an account on crates.io to acquire an API token
  2. Then run cargo login <token> with that API token and cargo publish

📖 This is how it describes on Cargo Docs with more details.

  • You’ll need an account on crates.io to acquire an API token. To do so, visit the home page and log in via a GitHub account (required for now). After this, visit your Account Settings page and run the cargo login command specified. +Ex. cargo login abcdefghijklmnopqrstuvwxyz012345
  • The next step is to package up your crate into a format that can be uploaded to crates.io. For this we’ll use the cargo package sub-command.
  • Now, it can be uploaded to crates.io with the cargo publish command.
  • If you’d like to skip the cargo package step, the cargo publish sub-command will automatically package up the local crate if a copy isn’t found already.

The name of our crate is test_crate_hello_world. So it can be found on, +📦 https://crates.io/crates/test_crate_hello_world +📑 https://docs.rs/test_crate_hello_world

💯 crates.io supports readme files as well. To enable it, we have to add the readme field to Cargo.toml. Ex: readme="README.md"

🏗️ Okay then, Let’s see how we can use this from another crate.

// # Think we run,
+cargo new greetings
+
+// # It generates,
+greetings
+ ├── Cargo.toml
+ └── src
+    └── main.rs
+
+// # Think we modify following files,
+
+// 01. greetings/Cargo.toml
+[package]
+name = "greetings"
+version = "0.1.0"
+authors = ["Dumindu Madunuwan"]
+
+[dependencies]
+test_crate_hello_world = "0.1.0"
+
+// 02. greetings/src/main.rs
+extern crate test_crate_hello_world;
+
+fn main() {
+    println!("{}", test_crate_hello_world::hello());
+}
+

By default, Cargo looks dependencies on crates.io. So we have to add only the crate name and a version string to Cargo.toml and then run cargo build to fetch the dependencies and compile them.

\ No newline at end of file diff --git a/docs/docs/crates/og.jpg b/docs/docs/crates/og.jpg new file mode 100644 index 00000000..7846e6ab Binary files /dev/null and b/docs/docs/crates/og.jpg differ diff --git a/docs/docs/custom-error-types/index.html b/docs/docs/custom-error-types/index.html new file mode 100644 index 00000000..4f969e10 --- /dev/null +++ b/docs/docs/custom-error-types/index.html @@ -0,0 +1,228 @@ +Custom Error Types · Learning Rust

Custom Error Types

Rust allow us to create our own Err types. We call them “Custom Error Types”.

Error trait

As you know traits define the functionality a type must provide. But we don’t always need to define new traits for common functionalities, because Rust standard library provides reusable traits which can be implemented on our own types. While creating custom error types the std::error::Error trait helps us to convert any type to an Err type.

use std::fmt::{Debug, Display};
+
+pub trait Error: Debug + Display {
+    fn source(&self) -> Option<&(Error + 'static)> { ... }
+}
+

As we discussed under traits inheritance, a trait can be inherited from another traits. trait Error: Debug + Display means Error trait inherits from fmt::Debug and fmt::Display traits.

// traits inside Rust standard library core fmt module/ std::fmt
+pub trait Display {
+    fn fmt(&self, f: &mut Formatter) -> Result<(), Error>;
+}
+
+pub trait Debug {
+    fn fmt(&self, f: &mut Formatter) -> Result<(), Error>;
+}
+
  • Display

    • How should the end user see this error as a message/ user-facing output.
    • Usually print via println!("{}") or eprintln!("{}")
  • Debug

    • How should display the Err while debugging/ programmer-facing output.
    • Usually print via println!("{:?}") or eprintln!("{:?}")
    • To pretty-print, println!("{:#?}") or eprintln!("{:#?}") can be used.
  • source()

    • The lower-level source of this error, if any.
    • Optional.

First, let’s see how to implement std::error::Error trait on a simplest custom error type.

use std::fmt;
+
+// Custom error type; can be any type which defined in the current crate
+// 💡 In here, we use a simple "unit struct" to simplify the example
+struct AppError;
+
+// Implement std::fmt::Display for AppError
+impl fmt::Display for AppError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "An Error Occurred, Please Try Again!") // user-facing output
+    }
+}
+
+// Implement std::fmt::Debug for AppError
+impl fmt::Debug for AppError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{{ file: {}, line: {} }}", file!(), line!()) // programmer-facing output
+    }
+}
+
+// A sample function to produce an AppError Err
+fn produce_error() -> Result<(), AppError> {
+    Err(AppError)
+}
+
+fn main() {
+    match produce_error() {
+        Err(e) => eprintln!("{}", e), // An Error Occurred, Please Try Again!
+        _ => println!("No error"),
+    }
+
+    eprintln!("{:?}", produce_error()); // Err({ file: src/main.rs, line: 17 })
+}
+

Hope you understood the main points. Now, let’s see a custom error type with an error code and an error message.

use std::fmt;
+
+struct AppError {
+    code: usize,
+    message: String,
+}
+
+// Different error messages according to AppError.code
+impl fmt::Display for AppError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        let err_msg = match self.code {
+            404 => "Sorry, Can not find the Page!",
+            _ => "Sorry, something is wrong! Please Try Again!",
+        };
+
+        write!(f, "{}", err_msg)
+    }
+}
+
+// A unique format for dubugging output
+impl fmt::Debug for AppError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(
+            f,
+            "AppError {{ code: {}, message: {} }}",
+            self.code, self.message
+        )
+    }
+}
+
+fn produce_error() -> Result<(), AppError> {
+    Err(AppError {
+        code: 404,
+        message: String::from("Page not found"),
+    })
+}
+
+fn main() {
+    match produce_error() {
+        Err(e) => eprintln!("{}", e), // Sorry, Can not find the Page!
+        _ => println!("No error"),
+    }
+
+    eprintln!("{:?}", produce_error()); // Err(AppError { code: 404, message: Page not found })
+
+    eprintln!("{:#?}", produce_error());
+    // Err(
+    //     AppError { code: 404, message: Page not found }
+    // )
+}
+

⭐️ Rust standard library provides not only reusable traits and also it facilitates to magically generate implementations for few traits via #[derive] attribute. Rust support derive std::fmt::Debug, to provide a default format for debug messages. So we can skip std::fmt::Debug implementation for custom error types and use #[derive(Debug)] before struct declaration.

For a struct #[derive(Debug)] prints, the name of the struct , { , comma-separated list of each field’s name and debug value and }.

use std::fmt;
+
+#[derive(Debug)] // derive std::fmt::Debug on AppError
+struct AppError {
+    code: usize,
+    message: String,
+}
+
+impl fmt::Display for AppError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        let err_msg = match self.code {
+            404 => "Sorry, Can not find the Page!",
+            _ => "Sorry, something is wrong! Please Try Again!",
+        };
+
+        write!(f, "{}", err_msg)
+    }
+}
+
+fn produce_error() -> Result<(), AppError> {
+    Err(AppError {
+        code: 404,
+        message: String::from("Page not found"),
+    })
+}
+
+fn main() {
+    match produce_error() {
+        Err(e) => eprintln!("{}", e), // Sorry, Can not find the Page!
+        _ => println!("No error"),
+    }
+
+    eprintln!("{:?}", produce_error()); // Err(AppError { code: 404, message: Page not found })
+
+    eprintln!("{:#?}", produce_error());
+    // Err(
+    //     AppError {
+    //         code: 404,
+    //         message: "Page not found"
+    //     }
+    // )
+}
+

From trait

When writing real programs, we mostly have to deal with different modules, different std and third party crates at the same time. Each crate uses their own error types. However, if we are using our own error type, we should convert those errors into our error type. For these conversions, we can use the standardized trait std::convert::From.

// traits inside Rust standard library core convert module/ std::convert
+pub trait From<T>: Sized {
+  fn from(_: T) -> Self;
+}
+

💡 As you know, String::from() function is used to create a String from &str data type. Actually this also an implementation of std::convert::From trait.

Let’s see how to implement std::convert::From trait on a custom error type.

use std::fs::File;
+use std::io;
+
+#[derive(Debug)]
+struct AppError {
+    kind: String,    // type of the error
+    message: String, // error message
+}
+
+// Implement std::convert::From for AppError; from io::Error
+impl From<io::Error> for AppError {
+    fn from(error: io::Error) -> Self {
+        AppError {
+            kind: String::from("io"),
+            message: error.to_string(),
+        }
+    }
+}
+
+fn main() -> Result<(), AppError> {
+    let _file = File::open("nonexistent_file.txt")?; // This generates an io::Error. But because of return type is Result<(), AppError>, it converts to AppError
+
+    Ok(())
+}
+
+
+// --------------- Run time error ---------------
+Error: AppError { kind: "io", message: "No such file or directory (os error 2)" }
+

In the above example, File::open(“nonexistent.txt”)? produces std::io::Error. But because of the return type is Result<(), AppError>, it converts to an AppError. Because of we are propagating the error from main() function, it prints the Debug representation of the Err.

In the above example we deal with only one std error type, std::io::Error. Let’s see some example which handles multiple std error types.

use std::fs::File;
+use std::io::{self, Read};
+use std::num;
+
+#[derive(Debug)]
+struct AppError {
+    kind: String,
+    message: String,
+}
+
+// Implement std::convert::From for AppError; from io::Error
+impl From<io::Error> for AppError {
+    fn from(error: io::Error) -> Self {
+        AppError {
+            kind: String::from("io"),
+            message: error.to_string(),
+        }
+    }
+}
+
+// Implement std::convert::From for AppError; from num::ParseIntError
+impl From<num::ParseIntError> for AppError {
+    fn from(error: num::ParseIntError) -> Self {
+        AppError {
+            kind: String::from("parse"),
+            message: error.to_string(),
+        }
+    }
+}
+
+fn main() -> Result<(), AppError> {
+    let mut file = File::open("hello_world.txt")?; // generates an io::Error, if can not open the file and converts to an AppError
+
+    let mut content = String::new();
+    file.read_to_string(&mut content)?; // generates an io::Error, if can not read file content and converts to an AppError
+
+    let _number: usize;
+    _number = content.parse()?; // generates num::ParseIntError, if can not convert file content to usize and converts to an AppError
+
+    Ok(())
+}
+
+
+// --------------- Few possible run time errors ---------------
+
+// 01. If hello_world.txt is a nonexistent file
+Error: AppError { kind: "io", message: "No such file or directory (os error 2)" }
+
+// 02. If user doesn't have relevant permission to access hello_world.txt
+Error: AppError { kind: "io", message: "Permission denied (os error 13)" }
+
+// 03. If hello_world.txt contains non-numeric content. ex Hello, world!
+Error: AppError { kind: "parse", message: "invalid digit found in string" }
+

🔎 Search about the implementation of std::io::ErrorKind, to see how to organize error types further.

\ No newline at end of file diff --git a/docs/docs/custom-error-types/og.jpg b/docs/docs/custom-error-types/og.jpg new file mode 100644 index 00000000..d13db674 Binary files /dev/null and b/docs/docs/custom-error-types/og.jpg differ diff --git a/docs/docs/enums/index.html b/docs/docs/enums/index.html new file mode 100644 index 00000000..c76d3810 --- /dev/null +++ b/docs/docs/enums/index.html @@ -0,0 +1,144 @@ +Enums · Learning Rust

Enums

  • An enum is a single type that contains variants, which represent the possible values of the enum at any given time.
  • By convention, the enum name and its variants’ names should follow PascalCase.
  • Can access the variants using the :: notation and the variant name. ex. Day::Sunday
enum Day {
+    Sunday,
+    Monday,
+    Tuesday,
+    Wednesday,
+    Thursday,
+    Friday,
+    Saturday,
+}
+
+// 💡 Day is the enum. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday are its variants.
+
  • An enum variant can have either,
    • No data (a unit variant)
    • Unnamed ordered data (a tuple variant)
    • Named data/ fields (a struct variant)
    enum FlashMessage {
    +    Success, // 💡 A unit variant (no data)
    +    Error(u8, String), // 💡 A tuple variant (one or more , separated data)
    +    Warning { field: String, message: String }, // 💡 A struct variant (one or more , separated name: value data)
    +}
    +
    +// 💡 FlashMessage is the emnum, Success, Error, Warning are its variants.
    +

💡 In Rust, the term “instantiation” is used to describe the act of creating a concrete instance of a type (struct or enum).

💡 In Rust, the term “field” is used to describe a named component in a C-like struct & struct-like enum variant, and the term “element” is used to describe an unnamed component in a tuple struct & tuple-like enum variant. The term “member” is used to describe both.

💯 More complex examples can be found on Generics, Impls and Traits, Lifetimes and Modules sections.

Instantiation

#![allow(unused)] // 💡 skip unused warnings, as we don't read fields in the enums
+
+#[derive(Debug)]
+enum FlashMessage { // Definition
+    Success,
+    Error(u32, String),
+    Warning { field: String, message: String },
+}
+
+fn main() {
+    // 1. Instantiation with separate variable declaration and assignment
+    let x: FlashMessage; // Declaration with the data type
+    x = FlashMessage::Success;
+    println!("{x:?}"); // Success
+    
+    // 2. Instantiation with a direct variable initialization
+    let a = FlashMessage::Success;
+    let b = FlashMessage::Error(401, "Unauthorized".to_string());
+    let c = FlashMessage::Warning { field: "email".to_string(), message: "This is required".to_string() };
+
+    println!("{a:?}"); // Success
+    println!("{b:?}"); // Error(401, "Unauthorized")
+    println!("{c:?}"); // Warning { field: "email", message: "This is required" }    
+}
+
// 3. Instantiation with a default variant
+#![allow(unused)] // 💡 skip unused warnings, as we don't use the all variants of the enum
+
+#[derive(Debug, Default)]
+enum Hand {
+    Left,
+    #[default] // 💡Set Right as the default variant
+    Right,
+}
+
+fn main() {
+    let a = Hand::default(); // Instantiation with the default variant
+    println!("{a:?}"); // Right
+}
+

In Rust, the #[derive()] attribute is used to automatically generate an implementation of certain traits for a custom data structure (struct and enum), instead of you writing them by hand. The std::fmt::Debug trait allows us to format a value with {:?} or {:#?} in println! and similar macros. The std::default::Default trait allows us to create a new instance of a type with the Type::default() method.

Pattern Matching

With match

#![allow(unused)] // 💡 skip unused warnings, as we don't use the all variants of the enum
+
+enum Season {
+    Spring,
+    Summer,
+    Autumn,
+    Winter,
+}
+
+fn main() {
+    let a = Season::Winter;
+    let result = match a {
+        Season::Spring => "☀️",
+        Season::Summer => "🍁",
+        Season::Autumn => "🍂",
+        Season::Winter => "❄️",
+    };
+
+    println!("{result}"); // ❄️
+}
+

With if let, else if let, else

if let is useful when we only care about handling one (or few) specific patterns and don’t need to explicitly match every possible case.

#![allow(unused)] // 💡 skip unused warnings, as we don't use the all variants of the enum
+
+enum Season {
+    Spring,
+    Summer,
+    Autumn,
+    Winter,
+}
+
+fn main() {
+    let a = Season::Winter;
+    let result = if let Season::Spring = a {
+        "☀️"
+    } else if let Season::Summer = a {
+        "🍁"
+    } else if let Season::Autumn = a {
+        "🍂"
+    } else if let Season::Winter = a {
+        "❄️"
+    } else {
+        unreachable!()
+    };
+
+    println!("{result}"); // ❄️
+}
+

Destructuring & Accessing Variants’ Members

In Rust, directly accessing an enum variant’s fields without any form of pattern matching is not possible. We need to use pattern matching to access the fields by using a match expression or if let expression.

With match

#![allow(unused)] // 💡 skip unused warnings, as we don't use the all variants of the enum
+
+enum FlashMessage {
+    Success,
+    Error(u32, String),
+    Warning { field: String, message: String },
+}
+
+fn main() {
+    let a = FlashMessage::Error(401, "Unauthorized".to_string());
+
+    let result = match a {
+        FlashMessage::Success => "We'll get back to you.".to_string(),
+        FlashMessage::Error(_, msg) => msg, // 💡 Destructuring only the second element of the tuple variant.
+        FlashMessage::Warning { message, .. } => message, // 💡 Destructuring only the second field of the struct variant.
+    };
+
+    println!("{result}"); // Unauthorized
+}
+

With if let, else if let, else

if let is useful when we only care about handling one (or few) specific patterns and don’t need to explicitly match every possible case.

#![allow(dead_code)] // 💡 Remove dead_code warnings, as we don't access the all elements of variants.
+
+enum FlashMessage {
+    Success,
+    Error(u32, String),
+    Warning { field: String, message: String },
+}
+
+fn main() {
+    let a = FlashMessage::Error(401, "Unauthorized".to_string());
+
+    if let FlashMessage::Error(_, msg) = a {
+        println!("{msg}"); // Unauthorized
+    } else if let FlashMessage::Warning { message, .. } = a {
+        println!("{message}");
+    } else {
+        println!("We'll get back to you.");
+    }
+}
+
\ No newline at end of file diff --git a/docs/docs/enums/og.jpg b/docs/docs/enums/og.jpg new file mode 100644 index 00000000..689e982e Binary files /dev/null and b/docs/docs/enums/og.jpg differ diff --git a/docs/docs/error-and-none-propagation/index.html b/docs/docs/error-and-none-propagation/index.html new file mode 100644 index 00000000..d02186bb --- /dev/null +++ b/docs/docs/error-and-none-propagation/index.html @@ -0,0 +1,73 @@ +Error and None Propagation · Learning Rust

Error and None Propagation

We should use panics like panic!(), unwrap(), expect() only if we can not handle the situation in a better way. Also if a function contains expressions which can produce either None or Err,

  • we can handle them inside the same function. Or,
  • we can return None and Err types immediately to the caller. So the caller can decide how to handle them.

💡 None types no need to handle by the caller of the function always. But Rusts’ convention to handle Err types is, return them immediately to the caller to give more control to the caller to decide how to handle them.

? Operator

  • If an Option type has Some value or a Result type has a Ok value, the value inside them passes to the next step.
  • If the Option type has None value or the Result type has Err value, return them immediately to the caller of the function.

Example with Option type,

fn main() {
+    if complex_function().is_none() {
+        println!("X not exists!");
+    }
+}
+
+fn complex_function() -> Option<&'static str> {
+    let x = get_an_optional_value()?; // if None, returns immediately; if Some("abc"), set x to "abc"
+
+    // some other code, ex
+    println!("{}", x); // "abc" ; if you change line 19 `false` to `true` 
+
+    Some("")
+}
+
+fn get_an_optional_value() -> Option<&'static str> {
+
+    //if the optional value is not empty
+    if false {
+        return Some("abc");
+    }
+    
+    //else
+    None
+}
+

Example with Result Type,

fn main() {
+    // `main` function is the caller of `complex_function` function
+    // So we handle errors of complex_function(), inside main()
+    if complex_function().is_err() {
+        println!("Can not calculate X!");
+    }
+}
+
+fn complex_function() -> Result<u64, String> {
+    let x = function_with_error()?; // if Err, returns immediately; if Ok(255), set x to 255
+
+    // some other code, ex
+    println!("{}", x); // 255 ; if you change line 20 `true` to `false`
+
+    Ok(0)
+}
+
+fn function_with_error() -> Result<u64, String> {
+    //if error happens
+    if true {
+        return Err("some message".to_string());
+    }
+
+    // else, return valid output
+    Ok(255)
+}
+

try!()

? operator was added in Rust version 1.13. try!() macro is the old way to propagate errors before that. So we should avoid using this now.

  • If a Result type has Ok value, the value inside it passes to the next step. If it has Err value, returns it immediately to the caller of the function.
// using `?`
+let x = function_with_error()?; // if Err, returns immediately; if Ok(255), set x to 255
+
+// using `try!()`
+let x = try!(function_with_error());
+

Error propagation from main()

Before Rust version 1.26, we couldn’t propagate Result and Option types from the main() function. But now, we can propagate Result types from the main() function and it prints the Debug representation of the Err.

💡 We are going to discuss about Debug representations under Error trait section.

use std::fs::File;
+
+fn main() -> std::io::Result<()> {
+    let _ = File::open("not-existing-file.txt")?;
+
+    Ok(()) // Because of the default return value of Rust functions is an empty tuple/ ()
+}
+
+// Because of the program can not find not-existing-file.txt , it produces,
+//    Err(Os { code: 2, kind: NotFound, message: "No such file or directory" })
+// While propagating error, the program prints,
+//    Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
+

💯 If you want to know about the all kind of errors std::fs::File::open() can produce, check the error list on std::fs::OpenOptions.

\ No newline at end of file diff --git a/docs/docs/error-and-none-propagation/og.jpg b/docs/docs/error-and-none-propagation/og.jpg new file mode 100644 index 00000000..9f70457d Binary files /dev/null and b/docs/docs/error-and-none-propagation/og.jpg differ diff --git a/docs/docs/functions/index.html b/docs/docs/functions/index.html new file mode 100644 index 00000000..4d0025d7 --- /dev/null +++ b/docs/docs/functions/index.html @@ -0,0 +1,114 @@ +Functions · Learning Rust

Functions

Named Functions

  • Named functions are declared with the keyword fn
  • When using arguments, we must declare the data types.
  • By default, functions return an empty tuple/ (). If you want to return a value, the return type must be specified after ->

Hello world

fn main() {
+    println!("Hello, world!");
+}
+

Passing Arguments

fn print_sum(a: i8, b: i8) {
+    println!("sum is: {}", a + b);
+}
+

Returning Values

  • Without the return keyword. Only the last expression returns.

    fn plus_one(a: i32) -> i32 {
    +    a + 1
    +    // There is no ending ; in the above line.
    +    // It means this is an expression which equals to `return a + 1;`.
    +}
    +
  • With the return keyword.

    fn plus_two(a: i32) -> i32 {
    +    return a + 2;
    +    // Should use return keyword only on conditional/ early returns.
    +    // Using return keyword in the last expression is a bad practice.
    +}
    +

Function Pointers as a Data Type

fn main() {
+    let p1 = plus_one; // Without type declarations
+    let a = p1(5); // 6
+
+    let p1: fn(i32) -> i32 = plus_one; // With the type declarations
+    let b = p1(5); // 6
+}
+
+fn plus_one(i: i32) -> i32 {
+    i + 1
+}
+

Closures

  • Also known as anonymous functions or lambda functions.
  • The data types of arguments and returns are optional ⃰ⁱᵛ.

Example with a named function, before using closures.

fn main() {
+    let x = 2;
+    println!("{}", get_square_value(x));
+}
+
+fn get_square_value(i: i32) -> i32 {
+    i * i
+}
+

With Optional Type Annotations

fn main() {
+    let x = 2;
+    let square = |i: i32| -> i32 { // Input parameters are passed inside | | and expression body is wrapped within { }
+        i * i
+    };
+    println!("{}", square(x));
+}
+

Without Type Annotations

fn main() {
+    let x = 2;
+    let square = |i| i * i; // { } are optional for single-lined closures
+    println!("{}", square(x));
+}
+

With Optional Type Annotations; Define and Call Together

fn main() {
+    let x = 2;
+    let x_square = |i: i32| -> i32 { i * i }(x); // { } are mandatory while creating and calling same time.
+    println!("{}", x_square);
+}
+

Without Type Annotations; Define and Call Together

fn main() {
+    let x = 2;
+    let x_square = |i| -> i32 { i * i }(x); // ⭐️ The return type is mandatory.
+    println!("{}", x_square);
+}
+

Test Functions

  • Start the function name with the test_ prefix.
  • Add with the #[test] attribute, inside a tests module with the #[cfg(test)] attribute.
fn greet() -> String {
+    "Hello, world!".to_string()
+}
+
+#[cfg(test)]
+mod tests {
+    use super::greet; // 💡 Reimport the greet() function from the parent module.
+
+    #[test]
+    fn test_greet() { // The test function of greet()
+        assert_eq!("Hello, world!", greet());
+    }
+}
+

👨‍🏫 Before going to the next…

  • 💯 Usage of :: and . to call functions in different modules,

    💭 This is a quick reference about the usage of :: and . operators while calling functions. So, please don’t worry about structs, enums, traits, or impls for now. We will discuss them later.

    • Functions are standalone blocks of code, declare with the fn keyword.

    • Associated functions are functions that are associated with a particular data type such as structs, enums, or traits via an impl block.

    • Methods are associated functions with a receiver of self, &self, &mut self, self: Box<Self> etc.

    ⭐️ To call methods: use the . operator from an instance. ex. steve.intro_name()

    ⭐️ To call associated functions that are not methods: use the :: operator from the data type. ex. Person::new(), String::from()

    struct Person {
    +    name: String,
    +    company_name: String,
    +}
    +
    +impl Person { // 💡 impls are used to define functions in Rust structs, enums, etc.
    +    // 💡 The constructor (new` is a conventional name, not a keyword)
    +    fn new(name: String, company_name: String) -> Person { // an associated function and not a method
    +        Person { name, company_name }
    +    }
    +
    +    fn intro_name(&self) -> String { // 💡 a method
    +        format!("I'm {}", self.name) // 💡 access fields via `.` operator
    +    }
    +
    +    fn intro_company(&self) -> String { // 💡 a method
    +        format!("I'm from {}", self.company_name)
    +    }
    +}
    +
    +fn main() {
    +    // 💡 calling associated functions with `::` operator
    +    let steve = Person::new(String::from("Steve Jobs"), String::from("Apple"));
    +
    +    // 💡 calling methods with `.` operator
    +    println!("{}. {}.", steve.intro_name(), steve.intro_company()); // I'm Steve Jobs. I'm from Apple.
    +
    +    // ⭐️ methods are also associated functions. So, we can call them with `::` operator as well but need to pass the instance as a parameter.
    +    println!("{}. {}.", Person::intro_name(&steve), Person::intro_company(&steve)); // I'm Steve Jobs. I'm from Apple.
    +}
    +
    • Other than that, :: operator is used to call functions in different modules.
    mod my_mod {
    +    pub fn greet(name: &str) {
    +        println!("Hello, {name}!")
    +    }
    +}
    +
    +fn main() {
    +    my_mod::greet("Steve Jobs"); // Hello, Steve Jobs!
    +}
    +

    🔎 Refer path separator and member access operators for more information about the usage of the :: and . operators.

\ No newline at end of file diff --git a/docs/docs/functions/og.jpg b/docs/docs/functions/og.jpg new file mode 100644 index 00000000..85b584ed Binary files /dev/null and b/docs/docs/functions/og.jpg differ diff --git a/docs/docs/generics/index.html b/docs/docs/generics/index.html new file mode 100644 index 00000000..0c4a654e --- /dev/null +++ b/docs/docs/generics/index.html @@ -0,0 +1,96 @@ +Generics · Learning Rust

Generics

  • The core concept of generics is abstraction over types. They let us write one piece of code to operate with any data type without repeating ourselves to write separate versions for each type. At the compile time, Rust ensures the type safety and generates an optimized code for each concrete type used in the program.
  • Use an uppercase letter (T, U, …) or a PascalCase identifier for the data type.
    • Instead of x: u8 we use x: T.
    • Inform the compiler that T is a generic type by adding <T> at first.

With One Generic Type

struct Point<T> {
+    x: T,
+    y: T,
+}
+
+fn to_tuple<T>(x: T, y: T) -> (T, T) {
+    (x, y)
+}
+
+fn main() {
+    let a = Point { x: 0, y: 1 }; // a: Point<i32>
+    let b = to_tuple(a.x, a.y); // (i32, i32)
+    println!("{b:?}"); // (0, 1)
+
+    let c = Point { x: false, y: true }; // a: Point<bool>
+    let d = to_tuple(c.x, c.y); // (bool, bool)
+    println!("{d:?}"); // (false, true)
+}
+

With Multiple Generic Types

struct Point<T, U> {
+    x: T,
+    y: U,
+}
+
+fn to_shuffled_tuple<T, U>(x: T, y: U) -> (U, T) {
+    (y, x)
+}
+
+fn main() {
+    let a = Point { x: 1u8, y: true }; // a: Point<u8, bool>
+    let b = to_shuffled_tuple(a.x, a.y); // (bool, u8)
+    println!("{b:?}"); // (true, 1)
+}
+

On some occasions, the compiler cannot inter the type, and we have to specify the type when using the generic type. By the way, it’s good practice to specify the type on variables when using a generic implementation.

#[derive(Debug)]
+enum Data<K, V> {
+    Value(V),
+    KeyValue(K, V),
+}
+
+fn main() {
+    let a: Data<(), bool> = Data::Value(true); // ⭐️ The compiler can not inter the type here. We have to specify the type.
+    let b = Data::KeyValue(1, true); // The compiler can infer the type; i32, bool
+
+    println!("{a:?}"); // Value(true)
+    println!("{b:?}"); // KeyValue(1, true)
+}
+

👨‍🏫 Before going to the next…

  • Option and Result

    💭 This is a quick reference to Option and Result as enums. Please don’t worry too much about them for now, as we will discuss them in detail later in Error Handling—Option & Result.

    Many languages use null\ nil\ undefined types to represent empty outputs, and Exceptions to handle errors. Rust skips using both, especially to prevent issues like null pointer exceptions, sensitive data leakages through exceptions, etc. Option and Result types are two special generic enums defined in Rust’s standard library to deal with these cases.

    // An output can have either Some value or no value/ None.
    +enum Option<T> { // T is a generic and it can contain any type of value.
    +    Some(T),
    +    None,
    +}
    +
    +// A result can represent either success/ Ok or failure/ Err.
    +enum Result<T, E> { // T and E are generics. T can contain any type of value, E can be any error.
    +    Ok(T),
    +    Err(E),
    +}
    +

    An optional value can have either Some value or no value/ None ⇒ possibility of absence

    A result can represent either success/ Ok or failure/ Err ⇒ possibility of failure

    • Option

      struct Task {
      +    title: String,
      +    assignee: Option<Person>, // 💡 Instead of `assignee: Person`, we use `assignee: Option<Person>` as the assignee can be `None`.
      +}
      +
      fn get_id_by_username(username: &str) -> Option<usize> { // 💡 Instead of setting return type as `usize`, set it `Option<usize>`
      +    // if username can be found in the system, return userId
      +        return Some(userId); // 💡 Instead of return userId, return Some(userId)
      +
      +    // else
      +        None // 💡 The last return statement no need `return` keyword and ending `;`
      +}
      +
      +fn main() {
      +    let username = "anonymous";
      +    match get_id_by_username(username) { // 💡 We can use pattern matching to catch the relevant return type (Some/None)
      +        None => println!("User not found"),
      +        Some(i) => println!("User Id: {}", i),
      +    }
      +}
      +
    • Result

      fn get_word_count_from_file(file_name: &str) -> Result<u32, &str> { // 💡 Instead of setting return type as `u32`, set it `Result<u32, &str>`
      +    // if the file is not found on the system, return error
      +        return Err("File can not be found!"); // 💡 Instead panic/ break when the file can not be found; return Err(something)
      +
      +    // else, count and return the word count
      +        Ok(word_count) // 💡 Instead of return `word_count`, return `Ok(word_count)`
      +        // 💡 The last return statement no need `return` keyword and ending `;`
      +}
      +
      +fn main() {
      +    let mut file_name = "file_a";
      +    match get_word_count_from_file(file_name) { // 💡 We can use pattern matching to catch the relevant return type (Ok/Err)
      +        Ok(i) => println!("Word Count: {}", i),
      +        Err(e) => println!("Error: {}", e)
      +    }
      +}
      +
\ No newline at end of file diff --git a/docs/docs/generics/og.jpg b/docs/docs/generics/og.jpg new file mode 100644 index 00000000..a6bf91b1 Binary files /dev/null and b/docs/docs/generics/og.jpg differ diff --git a/docs/docs/hello-world/index.html b/docs/docs/hello-world/index.html new file mode 100644 index 00000000..a155e6c0 --- /dev/null +++ b/docs/docs/hello-world/index.html @@ -0,0 +1,57 @@ +Hello World · Learning Rust

Hello World

Hello, World!

fn main() {
+    println!("Hello, world!");
+}
+

fn means function. The main function is the beginning of every Rust program. +println!() prints text to the console and its ! indicates that it’s a macro rather than a function.

💡 Rust files should have .rs file extension and if you’re using more than one word for the file name, follow the snake_case convention.

  • Save the above code in file.rs , but it can be any name with .rs extension.
  • Compile it with rustc file.rs
  • Execute it with ./file on Linux and Mac or file.exe on Windows

Rust Playground

Rust Playground is a web interface for running Rust code.

Rust Playground

👨‍🏫 Before going to the next…

  • These are the other usages of the println!() macro,

    println!("{}, {}!", "Hello", "world"); // Hello, world!
    +
    +println!("{0}, {1}!", "Hello", "world"); // Hello, world!
    +
    +println!("{a}, {b}!", a = "Hello", b = "world"); // Hello, world!
    +
    +let (a, b) = ("Hello", "world"); // 💡 Two Variable bindings declare & initialize in one line.
    +println!("{a}, {b}!"); // Hello, world!
    +
    +println!(); // A new line
    +
    // Debug print and pretty-print debug print format specifiers
    +println!("{:?}", [1, 2, 3]); // [1, 2, 3]
    +
    +println!("{:#?}", [1, 2, 3]);
    +/*
    +    [
    +        1,
    +        2,
    +        3
    +    ]
    +*/
    +
    +// 💡 We can use the variable directly with the format specifier as well.
    +let a = [1, 2, 3];
    +
    +println!("{a:?}"); // similar to println!("{:?}", a);
    +println!("{a:#?}"); // similar to println!("{:#?}", a);
    +
  • Rust has a print!() macro as well.

    print!("Hello, world!\n"); // With new line
    +print!("Hello, world!"); // Without new line
    +
  • The format!() macro is used to store the formatted string.

    let a = format!("{}, {}!", "Hello", "world");
    +println!("{a}"); // Hello, world!
    +
  • Let’s play a bit more…

    println!("{}", "Hello, world!".to_uppercase()); // HELLO, WORLD!
    +println!("{}", "Hello, world!".to_lowercase()); // hello, world!
    +
    +println!("{}", "⭐️".repeat(3)); // ⭐️⭐️⭐️
    +
    +println!("{}", "Hello, world!".chars().count()); // 13
    +// 💡 For more accurate results, you should use a crate like unicode_segmentation that follows more accurate Unicode text segmentation standards.
    +
  • Different format specifiers.

    let a = 255;
    +println!("{a}"); // 255
    +
    +println!("{a:b}"); // Binary 💡 11111111
    +println!("{a:o}"); // Octal 💡 377
    +println!("{a:x}"); // LowerHex 💡 ff
    +println!("{a:X}"); // UpperHex 💡 FF
    +
    +println!("{a:0>5}"); // Add leading zeros till character lengh 5 💡 00255
    +println!("{a:0<5}"); // Add tailing zeros till character lengh 5 💡 25500
    +
  • Check the difference between macros and functions.

  • For more rustc commands, check the rustc --help command.

\ No newline at end of file diff --git a/docs/docs/hello-world/og.jpg b/docs/docs/hello-world/og.jpg new file mode 100644 index 00000000..8a117961 Binary files /dev/null and b/docs/docs/hello-world/og.jpg differ diff --git a/docs/docs/impls/index.html b/docs/docs/impls/index.html new file mode 100644 index 00000000..a9db088b --- /dev/null +++ b/docs/docs/impls/index.html @@ -0,0 +1,74 @@ +Impls · Learning Rust

Impls

  • Earlier, we discussed that structs and enums group related data, while impl blocks and traits add associated and shared behavior to the data.
  • Usage of Self vs self keywords:
    • Self: Refers to the type itself (the blueprint).
    • self: Refers to the instance of the type (the actual data).
      • 💯 This can be any form of self, &self, &mut self, self: Box<Self>, self: Pin<&mut Self>, etc.
  • There are multiple ways to implement a behavior for a type. We discuss only about the impl blocks with this article. The patterns involving traits are discussed under Traits.

Inherent impls

Implement associated functions, methods, and constants directly for a type.

⭐️ The implementation must be in the same crate as the type.

impl Type

struct Person {
+    name: String,
+}
+
+impl Person {
+    const GREET: &str = "Hello!";
+
+    fn greet(&self) -> String { // &self` is shorthand for self: &Self
+        format!("{} I am {}.", Self::GREET, self.name) // 💡Self to access type; self to access instance
+    }
+}
+
+fn main() {
+    let steve = Person { name: "Steve".to_string() };
+    println!("{}", steve.greet()) // Hello! I am Steve.
+}
+
  • Inside the impl block,
    • Constants belong to the type itself.
      • To access them, use the Self keyword or the type name.
      • Example: Self::GREET (preferred in Rust) or Person::GREET.
    • Methods (functions that access instance fields) must send the instance/ self as the first parameter.
      • This can be self, &self, &mut self, self: Box<Self>, self: Pin<&mut Self>, etc.) as the first parameter.

impl<T> Type<T>

struct Point<T> {
+    x: T,
+    y: T,
+}
+
+impl<T> Point<T> {
+    fn into_tuple(self) -> (T, T) {
+        (self.x, self.y)
+    }
+}
+
+fn main() {
+    let a = Point { x: 0, y: 1 }; // a: Point<i32>
+    let b = a.into_tuple(); // (0, 1)
+    println!("{b:?}")
+}
+
+// 💡 into_tuple() take self (not &self) and consumes the self / instance.
+// 💡 we can pass &self (as a reference) and use as_ prefix to borrow the instance.
+// fn as_tuple(&self) -> (&T, &T) { (&self.x, &self.y) }
+

Associated Functions & Methods

  • Associated functions:
    • Functions that are associated with a particular data type via the impl block.
    • Can call from the type with :: operator.
      • Person::new(), Vec::new(), String::from()
  • Methods:
    • Associated functions with a receiver of self, &self, &mut self, self: Box<Self>, self: Pin<&mut Self>, etc.
    • Can call from the instance with . operator or from the type (as methods are also associated functions) with :: operator, but we need to pass the instance as the first parameter always.
      • steve.greet() or Person::greet(&steve)
      • "hello".to_string() or String::to_string("hello")
struct Person {
+    name: String,
+    company_name: String,
+}
+
+impl Person {
+    // 💡 The constructor (new` is a conventional name, not a keyword)
+    fn new(name: String, company_name: String) -> Self { // an associated function and not a method
+        Self { name, company_name }
+    }
+
+    fn intro_name(&self) -> String { // a method
+        format!("I'm {}", self.name)
+    }
+
+    fn intro_company(&self) -> String { // a method
+        format!("I'm from {}", self.company_name)
+    }
+}
+
+fn main() {
+    // Call from the type with `::` operator
+    let steve = Person::new(String::from("Steve Jobs"), String::from("Apple"));
+
+    // Call from the instance with `.` operator
+    println!("{}. {}.", steve.intro_name(), steve.intro_company()); // I'm Steve Jobs. I'm from Apple.
+
+    // As methods are also associated functions; Call from the type with `::` operator and pass the instance as the first parameter
+    println!("{}. {}.", Person::intro_name(&steve), Person::intro_company(&steve)); // I'm Steve Jobs. I'm from Apple.
+}
+

We can use the type/ Person instead Self keyword in the new function. By the way, using the Self keyword is considered idiomatic in Rust.

fn new(name: String, company_name: String) -> Person {
+    Person { name, company_name }
+}
+

👨‍🏫 Before going to the next…

  • Familiarize with the conventional prefixes and suffixes used in methods names.
    PrefixPostfixself takenself type
    as_none&self or &mut selfany
    from_nonenoneany
    into_noneselfany
    is_none&mut self or &self or noneany
    to__mut&mut selfany
    to_not _mutselfCopy
    to_not _mut&selfnot Copy
\ No newline at end of file diff --git a/docs/docs/impls/og.jpg b/docs/docs/impls/og.jpg new file mode 100644 index 00000000..dd52ce0d Binary files /dev/null and b/docs/docs/impls/og.jpg differ diff --git a/docs/docs/index.html b/docs/docs/index.html new file mode 100644 index 00000000..d5acc52f --- /dev/null +++ b/docs/docs/index.html @@ -0,0 +1 @@ +https://learning-rust.github.io/docs/overview/ \ No newline at end of file diff --git a/docs/docs/index.xml b/docs/docs/index.xml new file mode 100644 index 00000000..68f19aca --- /dev/null +++ b/docs/docs/index.xml @@ -0,0 +1,453 @@ +Docs on Learning Rusthttps://learning-rust.github.io/docs/Recent content in Docs on Learning RustHugoen-USBorrowinghttps://learning-rust.github.io/docs/borrowing/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/borrowing/<p>In real life applications, most of the times we have to pass variable bindings to other functions or assign them to other variable bindings. In this case, we are <strong>referencing</strong> the original binding; <strong>borrow</strong> the data of it.</p> +<h2 id="what-is-borrowing">What is Borrowing?</h2> +<blockquote> +<p><a href="https://github.com/nikomatsakis/rust-tutorials-keynote/blob/master/Ownership%20and%20Borrowing.pdf" target="_blank" >Borrow (verb)</a><br> +To receive something with the promise of returning it.</p> +</blockquote> +<h2 id="shared--mutable-borrowings">Shared &amp; Mutable borrowings</h2> +<p>⭐️ There are two types of Borrowing,</p> +<ol> +<li> +<p><strong>Shared Borrowing</strong> <code>(&amp;T)</code></p> +<ul> +<li>A piece of data can be <strong>borrowed by a single or multiple users</strong>, but <strong>data should not be altered</strong>.</li> +</ul> +</li> +<li> +<p><strong>Mutable Borrowing</strong> <code>(&amp;mut T)</code></p>Cargo, Crates and Basic Project Structurehttps://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/<h2 id="cargo">Cargo</h2> +<p>Cargo is Rust’s built-in package manager and build system. It also supports the following actions,</p> +<table> + <thead> + <tr> + <th>Command</th> + <th>Action</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>cargo new</code></td> + <td>Create a new project</td> + </tr> + <tr> + <td><code>cargo init</code></td> + <td>Create a new project in an existing directory</td> + </tr> + <tr> + <td><code>cargo check</code></td> + <td>Verify the project compiles without errors</td> + </tr> + <tr> + <td><code>cargo build</code></td> + <td>Build the executable</td> + </tr> + <tr> + <td><code>cargo run</code></td> + <td>Build the executable and run</td> + </tr> + <tr> + <td><code>cargo clean</code></td> + <td>Remove the build system directories/ <code>target</code> directory</td> + </tr> + </tbody> +</table> +<blockquote> +<p>💡 The <code>cargo check</code> command verifies that the project compiles without errors, without producing an executable. +Thus, it is often faster than <code>cargo build</code>.</p>Combinatorshttps://learning-rust.github.io/docs/combinators/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/combinators/<h2 id="what-is-a-combinator">What is a combinator?</h2> +<ul> +<li> +<p>One meaning of “combinator” is a more informal sense referring to the <strong>combinator pattern</strong>, a style of organizing libraries centered around the idea of combining things. Usually there is <strong>some type T</strong>, some <strong>functions for constructing “primitive” values of type T</strong>, and some “<strong>combinators</strong>” which can <strong>combine values of type T</strong> in various ways to <strong>build up more complex values of type T</strong>. The other definition is <strong>&ldquo;function with no free variables&rdquo;</strong>. +__ <a href="https://wiki.haskell.org/Combinator" target="_blank" >wiki.haskell.org</a></p>Comments and Documenting the codehttps://learning-rust.github.io/docs/comments-and-documenting-the-code/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/comments-and-documenting-the-code/<h2 id="comments">Comments</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="c1">// Line comments +</span></span></span><span class="line"><span class="cl"><span class="cm">/* Block comments */</span><span class="w"> +</span></span></span></code></pre></div><p>Nested block comments are supported.</p> +<p>💡 <strong>By convention, try to avoid using block comments. Use line comments instead.</strong></p> +<h2 id="doc-comments">Doc Comments</h2> +<p><a href="https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/#cargo" >As we discussed</a>, we can generate the project documentation via <a href="https://doc.rust-lang.org/stable/rustdoc/" target="_blank" >rustdoc</a> by running the <strong><code>cargo doc</code></strong> command. It uses the doc comments to generate the documentation.</p> +<p>💡 Usually we are adding doc comments on library crates. Also, we can use <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet" target="_blank" >Markdown notations</a> inside the doc comments.</p>Control Flowshttps://learning-rust.github.io/docs/control-flows/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/control-flows/<h2 id="if---else-if---else">if - else if - else</h2> +<h3 id="if"><code>if</code></h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">13</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">if</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">18</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, child!&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// The code prints this +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="if-else"><code>if</code> <code>else</code></h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">if</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Even&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Odd&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// The code prints this +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-let-statements">With <code>let</code> Statements</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">age</span>: <span class="kt">u8</span> <span class="o">=</span><span class="w"> </span><span class="mi">13</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">is_below_eighteen</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">18</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// true +</span></span></span></code></pre></div><h3 id="if-else-if-else"><code>if</code> <code>else if</code> <code>else</code></h3> +<p>i. A simple example,</p>Crateshttps://learning-rust.github.io/docs/crates/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/crates/<p>💭 Crates are a bit similar to the packages in some other languages. Crates compile individually. If the crate has child file modules, those files will get merged with the crate file and compile as a single unit.</p> +<p>💭 A crate can produce an executable/ a binary or a library. <code>src/main.rs</code> is the crate root/ entry point for a binary crate and <code>src/lib.rs</code> is the entry point for a library crate.</p>Custom Error Typeshttps://learning-rust.github.io/docs/custom-error-types/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/custom-error-types/<p>Rust allow us to create our own <code>Err</code> types. We call them “<em>Custom Error Types</em>”.</p> +<h2 id="error-trait">Error trait</h2> +<p>As you know <strong>traits define the functionality a type must provide</strong>. But we don’t always need to define new traits for common functionalities, because Rust <strong>standard library provides reusable traits</strong> which can be implemented on our own types. While creating custom error types the <a href="https://doc.rust-lang.org/std/error/trait.Error.html" target="_blank" ><code>std::error::Error</code> trait</a> helps us to convert any type to an <code>Err</code> type.</p>Enumshttps://learning-rust.github.io/docs/enums/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/enums/<ul> +<li>An enum is a single type that contains variants, which represent the possible values of the enum at any given time.</li> +<li>By convention, the enum name and its variants&rsquo; names should follow <a href="https://en.wikipedia.org/wiki/Camel_case" target="_blank" ><code>PascalCase</code></a>.</li> +<li>Can access the variants using the <code>::</code> notation and the variant name. ex. Day::Sunday</li> +</ul> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">enum</span> <span class="nc">Day</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Sunday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Monday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Tuesday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Wednesday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Thursday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Friday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Saturday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 💡 Day is the enum. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday are its variants. +</span></span></span></code></pre></div><ul> +<li>An enum variant can have either, +<ul> +<li>No data (a unit variant)</li> +<li>Unnamed ordered data (a tuple variant)</li> +<li>Named data/ fields (a struct variant)</li> +</ul> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">enum</span> <span class="nc">FlashMessage</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Success</span><span class="p">,</span><span class="w"> </span><span class="c1">// 💡 A unit variant (no data) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Error</span><span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="nb">String</span><span class="p">),</span><span class="w"> </span><span class="c1">// 💡 A tuple variant (one or more , separated data) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Warning</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">field</span>: <span class="nb">String</span><span class="p">,</span><span class="w"> </span><span class="n">message</span>: <span class="nb">String</span> <span class="p">},</span><span class="w"> </span><span class="c1">// 💡 A struct variant (one or more , separated name: value data) +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 💡 FlashMessage is the emnum, Success, Error, Warning are its variants. +</span></span></span></code></pre></div></li> +</ul> +<blockquote> +<p>💡 In Rust, the term &ldquo;instantiation&rdquo; is used to describe the act of creating a concrete instance of a type (struct or enum).</p>Error and None Propagationhttps://learning-rust.github.io/docs/error-and-none-propagation/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/error-and-none-propagation/<p>We should use panics like <code>panic!()</code>, <code>unwrap()</code>, <code>expect()</code> only if we can not handle the situation in a better way. Also if a function contains expressions which can produce either <code>None</code> or <code>Err</code>,</p> +<ul> +<li>we can handle them inside the same function. Or,</li> +<li>we can return <code>None</code> and <code>Err</code> types immediately to the caller. So the caller can decide how to handle them.</li> +</ul> +<p>💡 <code>None</code> types no need to handle by the caller of the function always. But Rusts’ convention to handle <strong><code>Err</code></strong> types is, <strong>return them immediately to the caller to give more control to the caller to decide how to handle them.</strong></p>Functionshttps://learning-rust.github.io/docs/functions/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/functions/<h2 id="named-functions">Named Functions</h2> +<ul> +<li>Named functions are declared with the keyword <strong><code>fn</code></strong></li> +<li>When using <strong>arguments</strong>, we <strong>must declare the data types</strong>.</li> +<li>By default, functions <strong>return an empty <a href="https://learning-rust.github.io/docs/primitive-data-types/#tuple" >tuple</a>/ <code>()</code></strong>. If you want to return a value, the <strong>return type must be specified</strong> after <strong><code>-&gt;</code></strong></li> +</ul> +<h3 id="hello-world">Hello world</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="passing-arguments">Passing Arguments</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">print_sum</span><span class="p">(</span><span class="n">a</span>: <span class="kt">i8</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">i8</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;sum is: </span><span class="si">{}</span><span class="s">&#34;</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="returning-values">Returning Values</h3> +<ul> +<li> +<p>Without the <code>return</code> keyword. Only the last expression returns.</p>Genericshttps://learning-rust.github.io/docs/generics/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/generics/<ul> +<li>The core concept of generics is abstraction over types. They let us write one piece of code to operate with any data type without repeating ourselves to write separate versions for each type. At the compile time, Rust ensures the type safety and generates an optimized code for each concrete type used in the program.</li> +<li>Use an uppercase letter (<code>T</code>, <code>U</code>, &hellip;) or a <a href="https://en.wikipedia.org/wiki/Camel_case" target="_blank" ><code>PascalCase</code></a> identifier for the data type. +<ul> +<li>Instead of <code>x: u8</code> we use <code>x: T</code>.</li> +<li>Inform the compiler that <code>T</code> is a generic type by adding <code>&lt;T&gt;</code> at first.</li> +</ul> +</li> +</ul> +<h2 id="with-one-generic-type">With One Generic Type</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Point</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">y</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">to_tuple</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// a: Point&lt;i32&gt; +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">to_tuple</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// (i32, i32) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{b:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// (0, 1) +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">false</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">true</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// a: Point&lt;bool&gt; +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">to_tuple</span><span class="p">(</span><span class="n">c</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">.</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// (bool, bool) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{d:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// (false, true) +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h2 id="with-multiple-generic-types">With Multiple Generic Types</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Point</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">y</span>: <span class="nc">U</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">to_shuffled_tuple</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">U</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="n">U</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">1</span><span class="k">u8</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">true</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// a: Point&lt;u8, bool&gt; +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">to_shuffled_tuple</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// (bool, u8) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{b:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// (true, 1) +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>On some occasions, the compiler cannot inter the type, and we have to specify the type when using the generic type. By the way, it&rsquo;s good practice to specify the type on variables when using a generic implementation.</p>Hello Worldhttps://learning-rust.github.io/docs/hello-world/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/hello-world/<h2 id="hello-world">Hello, World!</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p><code>fn</code> means function. The <code>main</code> function is the beginning of every Rust program. +<code>println!()</code> prints text to the console and its <code>!</code> indicates that it’s a <a href="https://doc.rust-lang.org/book/ch19-06-macros.html" target="_blank" >macro</a> rather than a function.</p> +<blockquote> +<p>💡 Rust files should have <code>.rs</code> file extension and if you’re using more than one word for the file name, follow the <a href="https://en.wikipedia.org/wiki/Snake_case" target="_blank" >snake_case</a> convention.</p> +</blockquote> +<ul> +<li>Save the above code in <code>file.rs</code> , but it can be any name with <code>.rs</code> extension.</li> +<li>Compile it with <code>rustc file.rs</code></li> +<li>Execute it with <code>./file</code> on Linux and Mac or <code>file.exe</code> on Windows</li> +</ul> +<h2 id="rust-playground">Rust Playground</h2> +<p><a href="https://play.rust-lang.org/" target="_blank" >Rust Playground</a> is a web interface for running Rust code.</p>Implshttps://learning-rust.github.io/docs/impls/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/impls/<ul> +<li>Earlier, we discussed that structs and enums group related data, while impl blocks and traits add associated and shared behavior to the data.</li> +<li>Usage of <code>Self</code> vs <code>self</code> keywords: +<ul> +<li><code>Self</code>: Refers to the type itself (the blueprint).</li> +<li><code>self</code>: Refers to the instance of the type (the actual data). +<ul> +<li>💯 This can be any form of <code>self</code>, <code>&amp;self</code>, <code>&amp;mut self</code>, <code>self: Box&lt;Self&gt;</code>, <code>self: Pin&lt;&amp;mut Self&gt;</code>, etc.</li> +</ul> +</li> +</ul> +</li> +<li>There are multiple ways to implement a behavior for a type. We discuss only about the <code>impl</code> blocks with this article. The patterns involving traits are discussed under <a href="https://learning-rust.github.io/docs/traits" >Traits</a>.</li> +</ul> +<h2 id="inherent-impls">Inherent impls</h2> +<p>Implement associated functions, methods, and constants directly for a type.</p>Installationhttps://learning-rust.github.io/docs/installation/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/installation/<h2 id="rustup">Rustup</h2> +<p>There are many ways to install Rust on your system. For the moment the official way to install Rust is using <a href="https://rustup.rs/" target="_blank" >Rustup</a>.</p> +<p><a href="https://rust-lang.github.io/rustup/index.html" target="_blank" >📖</a> Rustup installs The Rust Programming Language from the official release channels, enabling you to easily switch between <strong>stable, beta, and nightly</strong> compilers and keep them updated. It also makes cross-compiling simpler with binary builds of the standard library for common platforms.</p> +<p><a href="https://rust-lang.github.io/rustup/installation/index.html" target="_blank" >📖</a> Rustup installs <code>rustc</code>, <code>cargo</code>, <code>rustup</code> and other standard tools to <strong>Cargo&rsquo;s <code>bin</code> directory</strong>. On Unix it is located at <code>$HOME/.cargo/bin</code> and on Windows at <code>%USERPROFILE%\.cargo\bin</code>. This is the same directory that <code>cargo install</code> will install Rust programs and Cargo plugins.</p>Lifetimeshttps://learning-rust.github.io/docs/lifetimes/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/lifetimes/<p>When we are dealing with references, we have to make sure that the referencing data stay alive until we stop using the references.</p> +<p>Think,</p> +<ul> +<li>We have a <strong>variable binding</strong>, <code>a</code>.</li> +<li>We are <strong>referencing</strong> the value of <code>a</code>, <strong>from another variable binding</strong> <code>x</code>. +We have to make sure that <strong><code>a</code> lives until we stop using <code>x</code></strong>.</li> +</ul> +<blockquote> +<p>🔎 <strong>Memory management</strong> is a form of resource management applied to computer memory. Up until the mid-1990s, the majority of programming languages used <strong>Manual Memory Management</strong> which <strong>requires the programmer to give manual instructions</strong> to identify and deallocate unused objects/ garbage. Around 1959 John McCarthy invented <strong>Garbage collection</strong>(GC), a form of <strong>Automatic Memory Management</strong>(AMM). It determines what memory is no longer used and frees it automatically instead of relying on the programmer. However <strong>Objective-C and Swift</strong> provide similar functionality through <strong>Automatic Reference Counting</strong>(ARC).</p>Moduleshttps://learning-rust.github.io/docs/modules/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/modules/<h2 id="01-in-the-same-file">01. In the same file</h2> +<p>Related code and data are grouped into a module and stored in the same file.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">mod</span> <span class="nn">greetings</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="c1">// ⭐️ By default, everything inside a module is private +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// ⭐️ So function has to be public to access from outside +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>Modules can also be nested.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">mod</span> <span class="nn">phrases</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">greetings</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>Private functions can be called from the same module or from a child module.</p>Operatorshttps://learning-rust.github.io/docs/operators/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/operators/<h2 id="arithmetic-operators">Arithmetic Operators</h2> +<p><code>+ - * / %</code></p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// 6 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// 4 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 10 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// ⭐️ 2 not 2.5 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 1 +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">5.0</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2.0</span><span class="p">;</span><span class="w"> </span><span class="c1">// 2.5 +</span></span></span></code></pre></div><h2 id="comparison-operators">Comparison Operators</h2> +<p><code>== != &lt; &gt; &lt;= &gt;=</code></p>Option and Resulthttps://learning-rust.github.io/docs/option-and-result/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/option-and-result/<h2 id="why-option-and-result">Why Option and Result?</h2> +<p>Many languages use <strong><code>null</code>\ <code>nil</code>\ <code>undefined</code> types</strong> to represent empty outputs, and <strong><code>Exceptions</code></strong> to handle errors. Rust skips using both, especially to prevent issues like <strong>null pointer exceptions, sensitive data leakages through exceptions</strong>, etc. Instead, Rust provides two special <strong>generic enums</strong>;<code>Option</code> and <code>Result</code> to deal with above cases.</p> +<blockquote> +<p>💭 In the previous sections, we have discussed about the basics of <a href="https://learning-rust.github.io/docs/enums" >enums</a>, <a href="https://learning-rust.github.io/docs/generics" >generics</a> and <a href="https://learning-rust.github.io/docs/generics/#generalizing-enums" ><code>Result</code> &amp; <code>Option</code> types</a>.</p>Overviewhttps://learning-rust.github.io/docs/overview/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/overview/<h2 id="about-me">About me</h2> +<blockquote> +<p>🧑‍💻 I am an expat working in Singapore as a Go Backend and DevOps Engineer. Feel free to reach out if you find any mistakes or anything that needs to be changed, including spelling or grammar errors. Alternatively, you can create a pull request, open an issue, or <a href="https://gist.github.com/dumindu/00a0be2d175ed5ff3bc3c17bbf1ca5b6" target="_blank" >share your awesome ideas in this gist</a>. Good luck with learning Rust!</p> +</blockquote> +<p><a href="https://github.com/learning-rust/learning-rust.github.io" target="_blank" ><img src="https://img.shields.io/github/stars/learning-rust/learning-rust.github.io?style=for-the-badge&amp;logo=rust&amp;label=learning-rust.github.io&amp;logoColor=333333&amp;labelColor=f9f9f9&amp;color=F46623" alt="learning-rust.github.io"></a> +<a href="https://learning-cloud-native-go.github.io" target="_blank" ><img src="https://img.shields.io/github/stars/learning-cloud-native-go/learning-cloud-native-go.github.io?style=for-the-badge&amp;logo=go&amp;logoColor=333333&amp;label=learning-cloud-native-go.github.io&amp;labelColor=f9f9f9&amp;color=00ADD8" alt="learning-cloud-native-go.github.io"></a></p> +<p><a href="https://github.com/dumindu" target="_blank" ><img src="https://img.shields.io/badge/dumindu-866ee7?style=for-the-badge&amp;logo=GitHub&amp;logoColor=333333&amp;labelColor=f9f9f9" alt="github.com"></a> +<a href="https://www.buymeacoffee.com/dumindu" target="_blank" ><img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-dumindu-FFDD00?style=for-the-badge&amp;logo=buymeacoffee&amp;logoColor=333333&amp;labelColor=f9f9f9" alt="buymeacoffee"></a></p> +<h2 id="overview">Overview</h2> +<p>This publication has its origins in the posts I authored on Medium at <a href="https://medium.com/learning-rust" target="_blank" >https://medium.com/learning-rust</a>. However, please note that I have ceased updating the Medium posts. All current and future updates, new content, code, and grammar fixes will be exclusively maintained and released here, <a href="https://learning-rust.github.io" target="_blank" >https://learning-rust.github.io</a>.</p>Ownershiphttps://learning-rust.github.io/docs/ownership/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/ownership/<p>We discussed in the <a href="https://learning-rust.github.io/docs/traits/#derive-traits" >Derive Traits</a>, the usage of <a href="https://doc.rust-lang.org/std/marker/trait.Copy.html" target="_blank" ><code>Copy</code> marker trait</a> and <a href="https://doc.rust-lang.org/std/clone/index.html" target="_blank" ><code>.clone()</code></a> with the below code.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="cp">#[derive(Debug, Clone, Copy)]</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">x</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">y</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{a:?}</span><span class="s">, </span><span class="si">{b:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>If we try to remove <code>Copy</code> derive on <code>Point</code> and run, we will get the following error while compiling.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="n">error</span><span class="p">[</span><span class="n">E0382</span><span class="p">]</span>: <span class="nc">borrow</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">value</span>: <span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">main</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">11</span>:<span class="mi">16</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">occurs</span><span class="w"> </span><span class="n">because</span><span class="w"> </span><span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="k">type</span> <span class="err">`</span><span class="n">Point</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">does</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">implement</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="err">`</span><span class="nb">Copy</span><span class="err">`</span><span class="w"> </span><span class="k">trait</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="mi">9</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">here</span><span class="w"> +</span></span></span></code></pre></div><p>This is because of Ownership, which is used to achieve Rust&rsquo;s memory safety.</p>Panickinghttps://learning-rust.github.io/docs/panicking/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/panicking/<h2 id="panic">panic!()</h2> +<ul> +<li>In some cases, when an error occurs we can not do anything to handle it, <strong>if the error is something which should not have happened</strong>. In other words, if it’s an <strong>unrecoverable error</strong>.</li> +<li>Also <strong>when we are not using a feature-rich debugger or proper logs</strong>, sometimes we need to <strong>debug the code by quitting the program from a specific line of code</strong> by printing out a specific message or a value of a variable binding to understand the current flow of the program. +For above cases, we can use <code>panic!</code> macro.</li> +</ul> +<p>⭐ <code>panic!()</code> runs <strong>thread based</strong>. One thread can be panicked, while other threads are running.</p>Primitive Data Typeshttps://learning-rust.github.io/docs/primitive-data-types/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/primitive-data-types/<h2 id="bool">bool</h2> +<p>true or false</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="kt">bool</span> <span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ⭐️ no TRUE, FALSE, 1, 0 +</span></span></span></code></pre></div><p>bool is a single byte(8 bits) in size.</p> +<h2 id="char">char</h2> +<p>A single Unicode scalar value</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sc">&#39;x&#39;</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="kt">char</span> <span class="o">=</span><span class="w"> </span><span class="sc">&#39;😎&#39;</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ⭐️ no &#34;x&#34;, only single quotes +</span></span></span></code></pre></div><p>Because of Unicode support, char is not a single byte, but four(32 bits).</p> +<h2 id="i8-i16-i32-i64-i128">i8, i16, i32, i64, i128</h2> +<p>8, 16, 32, 64 and 128 bit fixed sized signed(+/-) integer types</p>Smart Compilerhttps://learning-rust.github.io/docs/smart-compiler/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/smart-compiler/<h2 id="why-compiler">Why Compiler?</h2> +<p>The Rust compiler does the most significant job to prevent errors in Rust programs. It <strong>analyzes the code at compile-time</strong> and issues warnings, if the code does not follow memory management rules or lifetime annotations correctly.</p> +<p>For example,</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="cp">#[allow(unused_variables)]</span><span class="w"> </span><span class="c1">//💡 A lint attribute used to suppress the warning; unused variable: `b` +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">];</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{:?}</span><span class="s">&#34;</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ------ Compile-time error ------ +</span></span></span><span class="line"><span class="cl"><span class="n">error</span><span class="p">[</span><span class="n">E0382</span><span class="p">]</span>: <span class="nc">use</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">value</span>: <span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">main</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">6</span>:<span class="mi">22</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="mi">3</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">here</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="mi">4</span><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="mi">5</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{:?}</span><span class="s">&#34;</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="n">used</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="n">after</span><span class="w"> </span><span class="k">move</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">note</span>: <span class="nc">move</span><span class="w"> </span><span class="n">occurs</span><span class="w"> </span><span class="n">because</span><span class="w"> </span><span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="k">type</span> <span class="err">`</span><span class="n">std</span>::<span class="n">vec</span>::<span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">does</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">implement</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="err">`</span><span class="nb">Copy</span><span class="err">`</span><span class="w"> </span><span class="k">trait</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="n">error</span>: <span class="nc">aborting</span><span class="w"> </span><span class="n">due</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">previous</span><span class="w"> </span><span class="n">error</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="n">For</span><span class="w"> </span><span class="n">more</span><span class="w"> </span><span class="n">information</span><span class="w"> </span><span class="n">about</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">error</span><span class="p">,</span><span class="w"> </span><span class="kr">try</span><span class="w"> </span><span class="err">`</span><span class="n">rustc</span><span class="w"> </span><span class="o">--</span><span class="n">explain</span><span class="w"> </span><span class="n">E0382</span><span class="err">`</span><span class="p">.</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ⭐ instead using #[allow(unused_variables)], consider using &#34;let _b = a;&#34; in line 4. +</span></span></span><span class="line"><span class="cl"><span class="c1">// Also you can use &#34;let _ =&#34; to completely ignore return values +</span></span></span></code></pre></div><blockquote> +<p>💭 In the previous sections, we have discussed memory management concepts like <a href="https://learning-rust.github.io/docs/ownership" >ownership</a>, <a href="https://learning-rust.github.io/docs/borrowing" >borrowing</a>, <a href="https://learning-rust.github.io/docs/lifetimes" >lifetimes</a> and etc.</p>STD, Primitives and Preludeshttps://learning-rust.github.io/docs/std-primitives-and-preludes/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/std-primitives-and-preludes/<p>⭐️ In Rust, language elements are implemented by not only <strong><code>std</code> library</strong> crate but also <strong>compiler</strong> as well. Examples,</p> +<ul> +<li><strong><a href="https://doc.rust-lang.org/std/#primitives" target="_blank" >Primitives</a></strong>: Defined by the compiler and methods are implemented by <code>std</code> library directly on primitives.</li> +<li><strong><a href="https://doc.rust-lang.org/std/#macros" target="_blank" >Standard Macros</a></strong>: Defined by both compiler and <code>std</code></li> +</ul> +<p>The <strong><code>std</code></strong> library has been divided into <strong><a href="https://doc.rust-lang.org/std/#modules" target="_blank" >modules</a></strong>, according to the main areas each covered.</p> +<p>⭐️ While primitives are implemented by the <strong>compiler</strong>, the standard library implements the <strong>most useful methods</strong> directly on the primitive types. But some <strong>rarely useful language elements</strong> of some primitives are stored on relevant <strong><code>std</code> modules</strong>. This is why you can see <code>char</code>, <code>str</code> and integer types on both <a href="https://doc.rust-lang.org/std/#primitives" target="_blank" >primitives</a> and <a href="https://doc.rust-lang.org/std/#modules" target="_blank" ><code>std</code> modules</a>.</p>Structshttps://learning-rust.github.io/docs/structs/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/structs/<ul> +<li>Used to <strong>encapsulate related properties into one unified data type</strong>.</li> +<li>By convention, the name should follow <a href="https://en.wikipedia.org/wiki/Camel_case" target="_blank" ><code>PascalCase</code></a>.</li> +<li>3 variants, +<ul> +<li> +<p>C-like structs: One or more <code>,</code> separated <code>name: value pairs</code> enclosed in <code>{}</code></p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Color</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">red</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">green</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">blue</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div></li> +<li> +<p>Tuple structs: One or more <code>,</code> separated <code>values</code> enclosed in <code>()</code></p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Color</span><span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"> +</span></span></span></code></pre></div></li> +<li> +<p>Unit structs: A struct with no fields/ members</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Black</span><span class="p">;</span><span class="w"> +</span></span></span></code></pre></div></li> +</ul> +</li> +</ul> +<blockquote> +<p>⭐️ In Rust, data (attributes) and behavior (associated functions and methods) are placed separately. Structs and Enums are used to group related data, and impls and traits are used to add associated and shared behavior to that data.</p>Traitshttps://learning-rust.github.io/docs/traits/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/traits/<p>A trait is a contract that defines a set of behaviors or properties that a type must implement. It can contain associated types, constants, function or method signatures, and overridable default implementations.</p> +<h2 id="definition">Definition</h2> +<h3 id="with-no-associates">With No Associates</h3> +<p>💡 Mostly used to mark a type as having certain properties to allow in certain operations. Known as Marker Traits.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="nb">Sized</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-the-declarations-of-associates">With the Declarations of Associates</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">trait</span><span class="w"> </span><span class="n">Greet</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="no">PREFIX</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span> <span class="kt">str</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">type</span> <span class="nc">Item</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">fn</span> <span class="nf">greet</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">String</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-the-default-implementations-of-associates">With the Default Implementations of Associates</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">trait</span><span class="w"> </span><span class="n">Greet</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="no">PREFIX</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span> <span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="s">&#34;Hello&#34;</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">fn</span> <span class="nf">greet</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">String</span> <span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">format!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{}</span><span class="s">!&#34;</span><span class="p">,</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="bp">Self</span>::<span class="no">PREFIX</span><span class="p">))</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-supertraits">With Supertraits</h3> +<p>A trait must have to be implemented first before implementing the current trait.</p>Unwrap and Expecthttps://learning-rust.github.io/docs/unwrap-and-expect/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/unwrap-and-expect/<h2 id="unwrap">unwrap()</h2> +<ul> +<li>If an <code>Option</code> type has <strong><code>Some</code></strong> value or a <code>Result</code> type has a <strong><code>Ok</code></strong> value, <strong>the value inside them</strong> passes to the next step.</li> +<li>If the <code>Option</code> type has <strong><code>None</code></strong> value or the <code>Result</code> type has <strong><code>Err</code></strong> value, <strong>program panics</strong>; If <code>Err</code>, panics with the error message.</li> +</ul> +<p>The functionality is bit similar to the following codes, which are using <code>match</code> instead <code>unwrap()</code>.</p> +<p>Example with <code>Option</code> and <code>match</code>, before using <code>unwrap()</code></p>Usehttps://learning-rust.github.io/docs/use/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/use/<p>Let&rsquo;s see the main usages of the <code>use</code> keyword.</p> +<h2 id="01-bind-a-full-path-to-a-new-name">01. Bind a full path to a new name</h2> +<p>Mainly <code>use</code> keyword is used to bind a full path of an element to a new name. So the user doesn’t want to repeat the full path each time.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="c1">// -- Initial code without the `use` keyword -- +</span></span></span><span class="line"><span class="cl"><span class="k">mod</span> <span class="nn">phrases</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">greetings</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> </span><span class="c1">// Using full path +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// -- Usage of the `use` keyword -- +</span></span></span><span class="line"><span class="cl"><span class="c1">// 01. Create an alias for module +</span></span></span><span class="line"><span class="cl"><span class="k">use</span><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 02. Create an alias for module elements +</span></span></span><span class="line"><span class="cl"><span class="k">use</span><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 03. Customize names with the `as` keyword +</span></span></span><span class="line"><span class="cl"><span class="k">use</span><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">greet</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">greet</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h2 id="02-import-elements-to-scope">02. Import elements to scope</h2> +<p>Another common usage of <code>use</code> is importing elements to scope. Remember that, this is also a bit similar to creating an alias and using it instead of using the full path.</p>Variable bindings, Constants & Staticshttps://learning-rust.github.io/docs/variable-bindings-constants-and-statics/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/variable-bindings-constants-and-statics/<ul> +<li>Rust is a statically typed language; it checks data types at compile-time. But it doesn’t require you to actually type data types when declaring variable bindings. In that case, the compiler checks the usage and sets a better data type for it.</li> +<li>⭐️ For <strong>constants and statics, we must annotate the data type</strong>.</li> +<li>Types come after a <code>:</code> (colon) sign.</li> +<li>The naming convention for the variable bindings is using the <a href="https://en.wikipedia.org/wiki/Snake_case" target="_blank" ><code>snake_case</code></a>. But, for constants and statics, we should follow the <a href="https://en.wikipedia.org/wiki/Snake_case" target="_blank" ><code>SCREAMING_SNAKE_CASE</code></a>.</li> +</ul> +<blockquote> +<p>💭 In the following examples, we will use <a href="https://learning-rust.github.io/docs/primitive-data-types" >data types</a> like <code>bool</code>, <code>i32</code>, <code>i64</code> and <code>f64</code>. Don&rsquo;t worry about them for now; they&rsquo;ll be discussed later.</p>Vectorshttps://learning-rust.github.io/docs/vectors/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/vectors/<p>If you remember, the array is a fixed-size list of elements, of the same data type. Even with mut, its element count cannot be changed. A vector is <strong>kind of a re-sizable array</strong> but <strong>all elements must be in the same type</strong>.</p> +<blockquote> +<p>💡 <code>Vec&lt;T&gt;</code>: capital “V” as <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html" target="_blank" >it’s a struct</a>.</p> +</blockquote> +<p>It’s a generic type, written as <strong><code>Vec&lt;T&gt;</code></strong>. T can have any type, ex. A vector of i32s is <code>Vec&lt;i32&gt;</code>. Also, Vectors always allocate their data in a dynamically allocated heap.</p>Why Rust?https://learning-rust.github.io/docs/why-rust/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/why-rust/<h2 id="history-of-rust">History of Rust</h2> +<p>Rust was initially designed and developed by former Mozilla employee <strong><a href="https://github.com/graydon" target="_blank" >Graydon Hoare</a></strong> as a personal project. Mozilla began sponsoring the project in 2009 and announced it in 2010. But the first stable release, Rust 1.0, was released on May 15, 2015.</p> +<p>Since Rust 1.0, major updates have been released as <a href="https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/#rust-editions" ><code>Editions</code></a> approximately every three years: Rust 2015 (with the release of Rust 1.0) , Rust 2018, Rust 2021, and Rust 2024, all maintaining backward compatibility.</p>Workspaceshttps://learning-rust.github.io/docs/workspaces/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/workspaces/<p>When the code base is getting larger, you might need to work with <strong>multiple crates on the same project</strong>. Rust supports this via Workspaces. You can <strong>analyze (<code>cargo check</code>), build, run tests or generate docs for all crates</strong> at once by running <code>cargo</code> commands from the project root.</p> +<p>⭐️ When working on multiple crates same time, there is a higher possibility of having shared dependencies on crates. To prevent downloading and compiling the same dependency multiple times, Rust uses a <strong>shared build directory</strong> under the project root, while running <code>cargo build</code> from the project root.</p> \ No newline at end of file diff --git a/docs/docs/installation/index.html b/docs/docs/installation/index.html new file mode 100644 index 00000000..3464f343 --- /dev/null +++ b/docs/docs/installation/index.html @@ -0,0 +1,6 @@ +Installation · Learning Rust

Installation

Rustup

There are many ways to install Rust on your system. For the moment the official way to install Rust is using Rustup.

📖 Rustup installs The Rust Programming Language from the official release channels, enabling you to easily switch between stable, beta, and nightly compilers and keep them updated. It also makes cross-compiling simpler with binary builds of the standard library for common platforms.

📖 Rustup installs rustc, cargo, rustup and other standard tools to Cargo’s bin directory. On Unix it is located at $HOME/.cargo/bin and on Windows at %USERPROFILE%\.cargo\bin. This is the same directory that cargo install will install Rust programs and Cargo plugins.

🔎 The main tools Rustup installs to the Cargo’s bin directory,

  • rustc: The Rust compiler.
  • cargo: The Rust’s built-in package manager and the build system.
  • rustup: The Rust toolchain installer.
  • rustfmt: The Rust’s official tool of formatting Rust code according to style guidelines.
  • cargo-fmt: Helps to run rustfmt on whole Rust projects, including multi-crate workspaces.
  • cargo-clippy: A lint tool that provides extra checks for common mistakes and stylistic choices.
  • cargo-miri:An experimental Rust interpreter, which can be used for checking for undefined-behavior.
  • rustdoc: A local copy of the Rust documentation.
  • rust-analyzer: A language server that provides support for editors and IDEs.
  • rust-gdb and rust-lldb: Rust debuggers that wrap the GNU Debugger(GDB) and Low-Level Debugger(LLDB).

Installation

For Mac and Linux Users

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
+

For Windows Users

Download rustup-init.exe from www.rustup.rs and run.

💡 You may need to install Visual C++ Build Tools 2019 or higher, which requires an additional 3–4 GBs.

👨‍🏫 Before going to the next…

  • To verify the current Rust version, use the rustc --version or shorter form rustc -V command.
  • Rust follows six week release cycles. Use the rustup update command to update the Rust ecosystem.
  • You can access Rust’s offline documentation via the rustup doc command.
  • For a full list of rustup commands, refer to the rustup --help command.
\ No newline at end of file diff --git a/docs/docs/installation/og.jpg b/docs/docs/installation/og.jpg new file mode 100644 index 00000000..79d39cd2 Binary files /dev/null and b/docs/docs/installation/og.jpg differ diff --git a/docs/docs/learning_rust_medium.png b/docs/docs/learning_rust_medium.png new file mode 100644 index 00000000..f2e7a465 Binary files /dev/null and b/docs/docs/learning_rust_medium.png differ diff --git a/docs/docs/lifetimes/index.html b/docs/docs/lifetimes/index.html new file mode 100644 index 00000000..628c5aa1 --- /dev/null +++ b/docs/docs/lifetimes/index.html @@ -0,0 +1,158 @@ +Lifetimes · Learning Rust

Lifetimes

When we are dealing with references, we have to make sure that the referencing data stay alive until we stop using the references.

Think,

  • We have a variable binding, a.
  • We are referencing the value of a, from another variable binding x. +We have to make sure that a lives until we stop using x.

🔎 Memory management is a form of resource management applied to computer memory. Up until the mid-1990s, the majority of programming languages used Manual Memory Management which requires the programmer to give manual instructions to identify and deallocate unused objects/ garbage. Around 1959 John McCarthy invented Garbage collection(GC), a form of Automatic Memory Management(AMM). It determines what memory is no longer used and frees it automatically instead of relying on the programmer. However Objective-C and Swift provide similar functionality through Automatic Reference Counting(ARC).

What is Lifetime?

In Rust,

  • A resource can only have one owner at a time. When it goes out of the scope, Rust removes it from the Memory.
  • When we want to reuse the same resource, we are referencing it/ borrowing its content.
  • When dealing with references, we have to specify lifetime annotations to provide instructions for the compiler to set how long those referenced resources should be alive.
  • ⭐ But because of lifetime annotations make the code more verbose, in order to make common patterns more ergonomic, Rust allows lifetimes to be elided/omitted in fn definitions. In this case, the compiler assigns lifetime annotations implicitly.

Lifetime annotations are checked at compile-time. The compiler checks when data is used for the first and the last times.

  • Unlike C and C++, usually, Rust doesn’t require explicitly dropping values at all.
  • Unlike GC, Rust doesn’t place deallocation calls where the data is no longer referenced.
  • Rust places deallocation calls where the data is about to go out of the scope and then enforces that no references to that resource exist after that point.

Usage

Lifetimes are denoted with an apostrophe. By convention, a lowercase letter is used for naming. Usually starts with 'a and follows alphabetic order when we need to add multiple lifetime annotations.

When using references,

01. On Function Declaration

  • Input and output parameters with references should attach lifetimes after the & sign. +ex. ..(x: &'a str) , ..(x: &'a mut str)

  • After the function name, we should mention that the given lifetimes are generic types. +ex. fn foo<'a>(..) , fn foo<'a, 'b>(..)

// No inputs, return a reference
+fn function<'a>() -> &'a str {}
+
+// Single input
+fn function<'a>(x: &'a str) {}
+
+// Single input and output, both have the same lifetime
+// The output should live at least as long as input exists
+fn function<'a>(x: &'a str) -> &'a str {}
+
+// Multiple inputs, only one input and the output share same lifetime
+// The output should live at least as long as y exists
+fn function<'a>(x: i32, y: &'a str) -> &'a str {}
+
+// Multiple inputs, both inputs and the output share same lifetime
+// The output should live at least as long as x and y exist
+fn function<'a>(x: &'a str, y: &'a str) -> &'a str {}
+
+// Multiple inputs, inputs can have different lifetimes 🔎
+// The output should live at least as long as x exists
+fn function<'a, 'b>(x: &'a str, y: &'b str) -> &'a str {}
+

02. On Struct or Enum Declaration

  • Elements with references should attach lifetimes after the & sign.
  • After the name of the struct or enum, we should mention that the given lifetimes are generic types.
// Single element
+// Data of x should live at least as long as Struct exists
+struct Struct<'a> {
+    x: &'a str
+}
+
+// Multiple elements
+// Data of x and y should live at least as long as Struct exists
+struct Struct<'a> {
+    x: &'a str,
+    y: &'a str
+}
+
+
+// Variant with a single element
+// Data of the variant should live at least as long as Enum exists
+enum Enum<'a> {
+    Variant(&'a Type)
+}
+

03. With Impls and Traits

struct Struct<'a> {
+    x: &'a str
+}
+    impl<'a> Struct<'a> {
+        fn function<'a>(&self) -> &'a str {
+            self.x
+        }
+    }
+
+
+struct Struct<'a> {
+    x: &'a str,
+    y: &'a str
+}
+    impl<'a> Struct<'a> {
+        fn new(x: &'a str, y: &'a str) -> Struct<'a> { // No need to specify <'a> after new; impl already has it
+          Struct {
+              x : x,
+              y : y
+          }
+        }
+    }
+
+
+// 🔎
+impl<'a> Trait<'a> for Type
+impl<'a> Trait for Type<'a>
+

04. With Generic Types

// 🔎
+fn function<F>(f: F) where for<'a> F: FnOnce(&'a Type)
+struct Struct<F> where for<'a> F: FnOnce(&'a Type) { x: F }
+enum Enum<F> where for<'a> F: FnOnce(&'a Type) { Variant(F) }
+impl<F> Struct<F> where for<'a> F: FnOnce(&'a Type) { fn x(&self) -> &F { &self.x } }
+

Lifetime Elision

As I mentioned earlier, in order to make common patterns more ergonomic, Rust allows lifetimes to be elided/omitted. This process is called Lifetime Elision.

💡 For the moment Rust supports Lifetime Elisions only on fn definitions. But in the future, it will support for impl headers as well.

Lifetime annotations of fn definitions can be elided
if its parameter list has either,

  • only one input parameter passes by reference.
  • a parameter with either &self or &mut self reference.
fn triple(x: &u64) -> u64 { // Only one input parameter passes by reference
+    x * 3
+}
+
+
+fn filter(x: u8, y: &str) -> &str { // Only one input parameter passes by reference
+    if x > 5 { y } else { "invalid inputs" }
+}
+
+
+struct Player<'a> {
+    id: u8,
+    name: &'a str
+}
+
+impl<'a> Player<'a> {
+    fn new(id: u8, name: &str) -> Player<'_> { // Only one input parameter passes by reference
+        Player {
+            id : id,
+            name : name
+        }
+    }
+
+    fn heading_text(&self) -> String { // An fn definition with &self (or &mut self) reference
+        format!("{}: {}", self.id, self.name)
+    }
+}
+
+fn main() {
+    let player1 = Player::new(1, "Serena Williams");
+    let player1_heading_text = player1.heading_text();
+    println!("{}", player1_heading_text);
+}
+

💡 In the Lifetime Elision process of fn definitions,

  • Each parameter passed by reference has got a distinct lifetime annotation. +ex. ..(x: &str, y: &str)..<'a, 'b>(x: &'a str, y: &'b str)
  • If the parameter list only has one parameter passed by reference, that lifetime is assigned to all elided lifetimes in the return values of that function. +ex. ..(x: i32, y: &str) -> &str..<'a>(x: i32, y: &'a str) -> &'a str
  • Even if it has multiple parameters passed by reference, if one of them has &self or &mut self, the lifetime of self is assigned to all elided output lifetimes. +ex. impl Impl{ fn function(&self, x: &str) -> &str {} } → +impl<'a> Impl<'a>{ fn function(&'a self, x: &'b str) -> &'a str {} }
  • For all other cases, we have to write lifetime annotations manually.

'static Annotations

'static lifetime annotation is a reserved lifetime annotation. These references are valid for the entire program. They are saved in the data segment of the binary and the data referred to will never go out of scope.

static N: i32 = 5; // A constant with 'static lifetime
+
+let a = "Hello, world."; // a: &'static str
+
+
+fn index() -> &'static str { // No need to mention <'static> ; fn index ̶<̶'̶s̶t̶a̶t̶i̶c̶>̶ 
+	"Hello, world!"
+}
+

Few more examples about the usage of Rust lifetimes.

fn greeting<'a>() -> &'a str {
+  "Hi!"
+}
+
+
+fn fullname<'a>(fname: &'a str, lname: &'a str) -> String {
+  format!("{} {}", fname, lname)
+}
+
+
+struct Person<'a> {
+    fname: &'a str,
+    lname: &'a str
+}
+  impl<'a> Person<'a> {
+      fn new(fname: &'a str, lname: &'a str) -> Person<'a> { // No need to specify <'a> after new; impl already has it
+          Person {
+              fname : fname,
+              lname : lname
+          }
+      }
+
+      fn fullname(&self) -> String {
+          format!("{} {}", self.fname , self.lname)
+      }
+  }
+
+fn main() {
+    let player = Person::new("Serena", "Williams");
+    let player_fullname = player.fullname();
+
+    println!("Player: {}", player_fullname);
+}
+
\ No newline at end of file diff --git a/docs/docs/lifetimes/og.jpg b/docs/docs/lifetimes/og.jpg new file mode 100644 index 00000000..dc02025c Binary files /dev/null and b/docs/docs/lifetimes/og.jpg differ diff --git a/docs/docs/modules/index.html b/docs/docs/modules/index.html new file mode 100644 index 00000000..2c645b2d --- /dev/null +++ b/docs/docs/modules/index.html @@ -0,0 +1,180 @@ +Modules · Learning Rust

Modules

01. In the same file

Related code and data are grouped into a module and stored in the same file.

fn main() {
+   greetings::hello();
+}
+
+mod greetings {
+  // ⭐️ By default, everything inside a module is private
+  pub fn hello() { // ⭐️ So function has to be public to access from outside
+    println!("Hello, world!");
+  }
+}
+

Modules can also be nested.

fn main() { 
+  phrases::greetings::hello();
+}
+
+mod phrases { 
+  pub mod greetings { 
+    pub fn hello() { 
+      println!("Hello, world!");
+    }
+  }
+}
+

Private functions can be called from the same module or from a child module.

// 01. Calling private functions of the same module
+fn main() {
+  phrases::greet();
+}
+
+mod phrases {
+  pub fn greet() {
+    hello(); // Or `self::hello();`
+  }
+  
+  fn hello() {
+    println!("Hello, world!");
+  }
+}
+
+// 02. Calling private functions of the parent module
+fn main() {
+  phrases::greetings::hello();
+}
+
+mod phrases {
+  fn private_fn() {
+    println!("Hello, world!");
+  }
+  
+  pub mod greetings {
+    pub fn hello() {
+      super::private_fn();
+    }
+  }
+}
+

💡 The self keyword is used to refer the same module, while the super keyword is used to refer parent module. Also, the super keyword can be used to access root functions from inside a module.

fn main() {
+  greetings::hello();
+}
+
+fn hello() {
+  println!("Hello, world!");
+}
+
+mod greetings {
+  pub fn hello() {
+    super::hello();
+  }
+}
+

🔎 When writing tests it’s a good practice to write tests inside a test module because they compile only when running tests.

fn greet() -> String {
+    "Hello, world!".to_string()
+}
+
+#[cfg(test)] // Only compiles when running tests
+mod tests {
+    use super::greet; // Import root greet function
+
+    #[test]
+    fn test_greet() {
+        assert_eq!("Hello, world!", greet());
+    }
+}
+

02. In a different file, same directory

// ↳ main.rs
+mod greetings; // Import greetings module
+
+fn main() {
+  greetings::hello();
+}
+
+// ↳ greetings.rs
+// ⭐️ No need to wrap the code with a mod declaration. The file itself acts as a module.
+pub fn hello() { // The function has to be public to access from outside
+  println!("Hello, world!");
+}
+

If we wrap file content with a mod declaration, it will act as a nested module.

// ↳ main.rs
+mod phrases;
+
+fn main() {
+  phrases::greetings::hello();
+}
+
+// ↳ phrases.rs
+pub mod greetings { // ⭐️ The module has to be public to access from outside
+  pub fn hello() {
+    println!("Hello, world!");
+  }
+}
+

03. In a different file, different directory

mod.rs in the directory module root is the entry point to the directory module. All other files in that directory root, act as sub-modules of the directory module.

// ↳ main.rs
+mod greetings;
+
+fn main() {
+  greetings::hello();
+}
+
+// ↳ greetings/mod.rs
+pub fn hello() { // ⭐️ The function has to be public to access from outside
+  println!("Hello, world!");
+}
+

Again, If we wrap file content with a mod declaration, it will act as a nested module.

// ↳ main.rs
+mod phrases;
+
+fn main() {
+  phrases::greetings::hello();
+}
+
+// ↳ phrases/mod.rs
+pub mod greetings { // ⭐️ The module has to be public to access from outside
+  pub fn hello() {
+    println!("Hello, world!");
+  }
+}
+

Other files in the directory module act as sub-modules for mod.rs.

// ↳ main.rs
+mod phrases;
+
+fn main() {
+  phrases::hello()
+}
+
+// ↳ phrases/mod.rs
+mod greetings;
+
+pub fn hello() {
+  greetings::hello()
+}
+
+// ↳ phrases/greetings.rs
+pub fn hello() {
+  println!("Hello, world!");
+}
+

⭐️ If you need to access an element of phrases/greetings.rs from outside the module, you have to import the greetings module as a public module.

// ↳ main.rs
+mod phrases;
+
+fn main() {
+    phrases::greetings::hello();
+}
+
+// ↳ phrases/mod.rs
+pub mod greetings;  // ⭐️ `pub mod` instead `mod`
+
+// ↳ phrases/greetings.rs
+pub fn hello() {
+  println!("Hello, world!");
+}
+

🔎 It’s unable to import child file modules of directory modules to main.rs, so you can’t use mod phrases::greetings; from main.rs. But there is a way to import phrases::greetings::hello() to phrases module by re-exporting hello to phrases module. So you can call it directly as phrases::hello().

// ↳ phrases/greetings.rs
+pub fn hello() {
+  println!("Hello, world!");
+}
+
+// ↳ phrases/mod.rs
+pub mod greetings;
+
+pub use self::greetings::hello; // Re-export `greetings::hello` to phrases
+
+// ↳ main.rs
+mod phrases;
+
+fn main() {
+    phrases::hello(); // You can call `hello()` directly from phrases
+}
+

This allows you to present an external interface that may not directly map to your internal code organization. If still it is not clear, don’t worry; We discuss the usages of use on an upcoming section in this post.

\ No newline at end of file diff --git a/docs/docs/modules/og.jpg b/docs/docs/modules/og.jpg new file mode 100644 index 00000000..d6639c26 Binary files /dev/null and b/docs/docs/modules/og.jpg differ diff --git a/docs/docs/operators/index.html b/docs/docs/operators/index.html new file mode 100644 index 00000000..c83768f4 --- /dev/null +++ b/docs/docs/operators/index.html @@ -0,0 +1,93 @@ +Operators · Learning Rust

Operators

Arithmetic Operators

+ - * / %

let a = 5;
+let b = a + 1; // 6
+let c = a - 1; // 4
+let d = a * 2; // 10
+let e = a / 2; // ⭐️ 2 not 2.5
+let f = a % 2; // 1
+
+let g = 5.0 / 2.0; // 2.5
+

Comparison Operators

== != < > <= >=

let a = 1;
+let b = 2;
+
+let c = a == b; // false
+let d = a != b; // true
+let e = a < b; // true
+let f = a > b; // false
+let g = a <= a; // true
+let h = a >= a; // true
+
+// 🔎
+let i = true > false; // true
+let j = 'a' > 'A'; // true
+

Logical Operators

! && ||

let a = true;
+let b = false;
+
+let c = !a; // false
+let d = a && b; // false
+let e = a || b; // true
+

🔎 On integer types,! inverts the individual bits in the two’s complement representation of the value.

let a = !-2; // 1
+let b = !-1; // 0
+let c = !0; // -1
+let d = !1; // -2
+

Bitwise Operators

& | ^ << >>

let a = 1;
+let b = 2;
+
+let c = a & b; // 0  (01 && 10 -> 00)
+let d = a | b; // 3  (01 || 10 -> 11)
+let e = a ^ b; // 3  (01 != 10 -> 11)
+let f = a << b; // 4  (Add b number of 0s to the end of a -> '01'+'00' -> 100)
+let g = a >> b; // 0  (Remove b number of bits from the end of a -> o̶1̶ -> 0)
+

Assignment and Compound Assignment Operators

The = operator is used to assign a name to a value or a function. Compound Assignment Operators are created by composing one of + - * / % & | ^ << >> operators with = operator.

let mut a = 2;
+
+a += 5; // 2 + 5 = 7
+a -= 2; // 7 - 2 = 5
+a *= 5; // 5 * 5 = 25
+a /= 2; // 25 / 2 = 12 not 12.5
+a %= 5; // 12 % 5 = 2
+
+a &= 2; // 10 && 10 -> 10 -> 2
+a |= 5; // 010 || 101 -> 111 -> 7
+a ^= 2; // 111 != 010 -> 101 -> 5
+a <<= 1; // '101'+'0' -> 1010 -> 10
+a >>= 2; // 101̶0̶ -> 10 -> 2
+

Type Casting Operator

as

let a = 15;
+let b = (a as f64) / 2.0; // 7.5
+

Borrowing and Dereference Operators

& &mut *

🔎 The & or &mut operators are used for borrowing and * operator for dereferencing. For more information, refer Ownership, Borrowing & Lifetimes sections.

Path Separator and Member Access Operators

:: .

🔎 We discussed the usage of the :: and . operators when calling associated functions and methods under Functions.

💯 Also, the :: operator is used to access modules, constants, structs, enums, functions (excluding methods) in std modules, crates or nested/ parent modules and to access variants of enums.

💯 Also, the . operator is used to access fields of tuples and structs and to call methods in datatypes.

use std::collections::HashMap; // 💡load HashMap struct from the std::collections module
+
+fn main() {
+    // 💡 using `HashMap::from()` associated function to create a HashMap
+    let best_movies = HashMap::from([
+        ("01", "The Shawshank Redemption"),
+        ("02", "The Godfather"),
+        ("03", "The Dark Knight"),
+    ]);
+
+    println!("{:?}", best_movies);
+}
+
println!("{}", std::primitive::u8::MAX); // 💡 access MAX constant from the std::primitive::u8
+
let mut a = (1, "Steve"); // 💡 a is a tuple
+
+// 💡 access fields via dot operator
+a.0 = 2;
+a.1 = "Tim";
+println!("{} {}", a.0, a.1); // 2 Tim
+

👨‍🏫 Before going to the next…

  • About string concatenation,

    let (s1, s2) = ("abc", "123"); // both &str
    +// All bellow codes return abc123 (in String data type)
    +
    +// 1. via + operator
    +let s = String::from(s1) + s2; // String + &str
    +
    +// 2. via push_str method
    +let mut s = String::from(s1);
    +s.push_str(s2); // String + &str
    +
    +// 3. via format! macro
    +let s = format!("{s1}{s2}"); // &str/String + &str/String
    +
    +// 4. via concat method
    +let s = [s1, s2].concat(); // &str or String array
    +
\ No newline at end of file diff --git a/docs/docs/operators/og.jpg b/docs/docs/operators/og.jpg new file mode 100644 index 00000000..90c3b98a Binary files /dev/null and b/docs/docs/operators/og.jpg differ diff --git a/docs/docs/option-and-result/index.html b/docs/docs/option-and-result/index.html new file mode 100644 index 00000000..30293d48 --- /dev/null +++ b/docs/docs/option-and-result/index.html @@ -0,0 +1,87 @@ +Option and Result · Learning Rust

Option and Result

Why Option and Result?

Many languages use null\ nil\ undefined types to represent empty outputs, and Exceptions to handle errors. Rust skips using both, especially to prevent issues like null pointer exceptions, sensitive data leakages through exceptions, etc. Instead, Rust provides two special generic enums;Option and Result to deal with above cases.

💭 In the previous sections, we have discussed about the basics of enums, generics and Result & Option types.

As you know,

  • An optional value can have either Some value or no value/ None.
  • A result can represent either success/ Ok or failure/ Err
// An output can have either Some value or no value/ None.
+enum Option<T> { // T is a generic and it can contain any type of value.
+    Some(T),
+    None,
+}
+
+// A result can represent either success/ Ok or failure/ Err.
+enum Result<T, E> { // T and E are generics. T can contain any type of value, E can be any error.
+    Ok(T),
+    Err(E),
+}
+

💭 Also as we discussed in preludes, not only Option and Result, and also their variants are in preludes. So, we can use them directly without using namespaces in the code.

Basic usages of Option

When writing a function or data type,

  • if an argument of the function is optional,
  • if the function is non-void and if the output it returns can be empty,
  • if the value of a property of the data type can be empty,

we have to use their data type as an Option type.

For example, if the function outputs a &str value and the output can be empty, the return type of the function should be set as Option<&str>.

fn get_an_optional_value() -> Option<&str> {
+
+    //if the optional value is not empty
+    return Some("Some value");
+    
+    //else
+    None
+}
+

In the same way, if the value of a property of a data type can be empty or optional like the middle_name of the Name data type in the following example, we should set its data type as an Option type.

struct Name {
+  first_name: String,
+  middle_name: Option<String>, // middle_name can be empty
+  last_name: String,
+}
+

💭 As you know, we can use pattern matching to catch the relevant return type (Some/ None) via match. There is a function in std::env called home_dir() to get the current user’s home directory. However, not all users have a home directory in systems like Linux, so the home directory of a user can be optional. So it returns an Option type; Option<PathBuf>.

use std::env;
+
+fn main() {
+    let home_path = env::home_dir();
+    match home_path {
+        Some(p) => println!("{:?}", p), // This prints "/root", if you run this in Rust playground
+        None => println!("Can not find the home directory!"),
+    }
+}
+

⭐ However, when using optional arguments with functions, we have to pass None values for empty arguments while calling the function.

fn get_full_name(fname: &str, lname: &str, mname: Option<&str>) -> String { // middle name can be empty
+  match mname {
+    Some(n) => format!("{} {} {}", fname, n, lname),
+    None => format!("{} {}", fname, lname),
+  }
+}
+
+fn main() {
+  println!("{}", get_full_name("Galileo", "Galilei", None));
+  println!("{}", get_full_name("Leonardo", "Vinci", Some("Da")));
+}
+
+// 💡 Better create a struct as Person with fname, lname, mname fields and create a impl function as full_name()
+

🔎 Other than that, Option types are used with nullable pointers in Rust. Because there are no null pointers in Rust, the pointer types should point to a valid location. So if a pointer can be nullable, we have use Option<Box<T>> .

Basic usages of Result

If a function can produce an error, we have to use a Result type by combining the data type of the valid output and the data type of the error. For example, if the data type of the valid output is u64 and error type is String, the return type should be Result<u64, String>.

fn function_with_error() -> Result<u64, String> {
+  
+    //if error happens
+    return Err("The error message".to_string());
+
+    // else, return valid output
+    Ok(255)
+}
+

💭 As you know, we can use the pattern matching to catch the relevant return types (Ok/Err) via match. There is a function to fetch the value of any environment variable in std::env called var(). Its input is the environment variable name. This can produce an error if we pass a wrong environment variable or the program cannot extract the value of the environment variable while running. So, its return type is a Result type; Result<String, VarError>.

use std::env;
+
+fn main() {
+    let key = "HOME";
+    match env::var(key) {
+        Ok(v) => println!("{}", v), // This prints "/root", if you run this in Rust playground
+        Err(e) => println!("{}", e), // This prints "environment variable not found", if you give a nonexistent environment variable
+    }
+}
+

is_some(), is_none(), is_ok(), is_err()

Other than match expressions, Rust provides is_some() , is_none() and is_ok() , is_err() functions to identify the return type.

fn main() {
+    let x: Option<&str> = Some("Hello, world!");
+    assert_eq!(x.is_some(), true);
+    assert_eq!(x.is_none(), false);
+
+    let y: Result<i8, &str> = Ok(10);
+    assert_eq!(y.is_ok(), true);
+    assert_eq!(y.is_err(), false);
+}
+

ok(), err() for Result types

In addition to that, Rust provides ok() and err() for Result types. They convert the Ok<T> and Err<E> values of a Result type to Option types.

fn main() {
+    let o: Result<i8, &str> = Ok(8);
+    let e: Result<i8, &str> = Err("message");
+    
+    assert_eq!(o.ok(), Some(8)); // Ok(v) ok = Some(v)
+    assert_eq!(e.ok(), None);    // Err(v) ok = None
+    
+    assert_eq!(o.err(), None);            // Ok(v) err = None
+    assert_eq!(e.err(), Some("message")); // Err(v) err = Some(v)
+}
+
\ No newline at end of file diff --git a/docs/docs/option-and-result/og.jpg b/docs/docs/option-and-result/og.jpg new file mode 100644 index 00000000..d03b979c Binary files /dev/null and b/docs/docs/option-and-result/og.jpg differ diff --git a/docs/docs/overview/index.html b/docs/docs/overview/index.html new file mode 100644 index 00000000..ce52ecb6 --- /dev/null +++ b/docs/docs/overview/index.html @@ -0,0 +1,7 @@ +Overview · Learning Rust

Overview

About me

🧑‍💻 I am an expat working in Singapore as a Go Backend and DevOps Engineer. Feel free to reach out if you find any mistakes or anything that needs to be changed, including spelling or grammar errors. Alternatively, you can create a pull request, open an issue, or share your awesome ideas in this gist. Good luck with learning Rust!

learning-rust.github.io +learning-cloud-native-go.github.io

github.com +buymeacoffee

Overview

This publication has its origins in the posts I authored on Medium at https://medium.com/learning-rust. However, please note that I have ceased updating the Medium posts. All current and future updates, new content, code, and grammar fixes will be exclusively maintained and released here, https://learning-rust.github.io.

Learning Rust @Medium

\ No newline at end of file diff --git a/docs/docs/overview/og.jpg b/docs/docs/overview/og.jpg new file mode 100644 index 00000000..0b73116f Binary files /dev/null and b/docs/docs/overview/og.jpg differ diff --git a/docs/docs/ownership/index.html b/docs/docs/ownership/index.html new file mode 100644 index 00000000..8e048343 --- /dev/null +++ b/docs/docs/ownership/index.html @@ -0,0 +1,139 @@ +Ownership · Learning Rust

Ownership

We discussed in the Derive Traits, the usage of Copy marker trait and .clone() with the below code.

#[derive(Debug, Clone, Copy)]
+struct Point {
+    x: i32,
+    y: i32,
+}
+
+fn main() {
+    let a = Point { x: 0, y: 1 };
+    let b = a;
+
+    println!("{a:?}, {b:?}");
+}
+

If we try to remove Copy derive on Point and run, we will get the following error while compiling.

error[E0382]: borrow of moved value: `a`
+  --> src/main.rs:11:16
+   |
+ 8 |     let a = Point { x: 0, y: 1 };
+   |         - move occurs because `a` has type `Point`, which does not implement the `Copy` trait
+ 9 |     let b = a;
+   |             - value moved here
+

This is because of Ownership, which is used to achieve Rust’s memory safety.

Ownership

In Rust,

  • Each piece of data has a single owner, and data is only scoped to its owner (💯 if it is not borrowed).
    • This means that when the owner of the data goes out of scope, the bound resource will be dropped/ released from memory.
  • There can only be one owner at a time. Assigning a variable to another variable or passing it to a function (if not passed by referencing) triggers one of the following behaviors:
    • Copy (for the types that implement the Copy trait):
      • The value is duplicated.
      • Both variables become independent owners of their own data and remain accessible.
    • Move (default behavior/ if not implemented Copy trait):
      • Ownership transfers to the new variable.
      • The original variable is invalidated and can no longer be used.

💡 In Rust, every assignment is technically a bitwise move (a memcpy). The difference is that if a type implements the Copy trait, the compiler simply doesn’t invalidate the original variable afterward.

Copy Types

Rust Standard Library has implemented the Copy trait inside std for primitive types and most simpler types mainly,

  • Due to the orphan rule, we are not allowed to implement std traits for std types.
  • To avoid needing to call the .clone() method each time when assigning data to a new variable or passing it to a function.

Also as we discussed, the Copy marker trait can be added to custom types via a derive macro. However, the Rust compiler will fail to compile if the type contains any non-Copy members.

#[derive(Debug, Clone, Copy)]
+struct Person {
+    name: String, // 💡 String is not a Copy type. It's a Move type.
+}
+
+fn main() {
+    let a = Person { name: "Steve".to_string() };
+    dbg!(a);
+}
+
error[E0204]: the trait `Copy` cannot be implemented for this type
+ --> src/main.rs:1:24
+  |
+1 | #[derive(Debug, Clone, Copy)]
+  |                        ^^^^
+2 | struct Person {
+3 |     name: String,
+  |     ------------ this field does not implement `Copy`
+

Implementations in std

  • Primitive types, arrays of Copy types, tuples of Copy types, raw pointers *const T/ *mut T, shared references/ &T

    marker_impls! {
    +    #[stable(feature = "rust1", since = "1.0.0")]
    +    Copy for
    +        usize, u8, u16, u32, u64, u128,
    +        isize, i8, i16, i32, i64, i128,
    +        f16, f32, f64, f128,
    +        bool, char,
    +        {T: PointeeSized} *const T,
    +        {T: PointeeSized} *mut T,
    +}
    +
    +#[stable(feature = "copy_clone_array_lib", since = "1.58.0")]
    +impl<T: Copy, const N: usize> Copy for [T; N] {}
    +
    +/// Shared references can be copied, but mutable references *cannot*!
    +#[stable(feature = "rust1", since = "1.0.0")]
    +impl<T: PointeeSized> Copy for &T {}
    +
  • When T, E, P are Copy types, then Option<T>, Result<T, E>, Pin<P>, Poll<T>

  • IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope, SocketAddr, SocketAddrV4, SocketAddrV6, SystemTime, Duration, Instant, PhantomData, and more

More Examples

#![allow(unused)]
+
+fn main() {
+    let a = [1, 2, 3]; // 💡 Array of Copy types
+    let b = a;
+
+    let c = (true, 'C', 32, 1.2); // 💡 Tuple of Copy types
+    let d = c;
+
+    let e = Color { red: 255, green: 0, blue: 0 }; // 💡 Type with Copy typed members + Copy marker
+    let f = e;
+
+    println!("{:?} {:?}", a, b); // [1, 2, 3] [1, 2, 3]
+    println!("{:?} {:?}", c, d); // (true, 'C', 32, 1.2) (true, 'C', 32, 1.2)
+    println!("{:?} {:?}", e, f); // Color { red: 255, green: 0, blue: 0 } Color { red: 255, green: 0, blue: 0 }
+}
+
+#[derive(Debug, Clone, Copy)]
+struct Color {
+    red: u8,
+    green: u8,
+    blue: u8,
+}
+
fn main() {
+    let x: [i32; 3];
+
+    {
+        let a = [1, 2, 3];
+        x = a;
+        println!("{:?}", a); // [1, 2, 3]
+        
+        // `a` is dropped here, but `x` holds its own independent copy
+    }
+
+    println!("{:?}", x); // [1, 2, 3]
+}
+

Move Types

By default, data in Rust follows move semantics unless the type implements the Copy marker trait.

Since Rust prohibits implementing Copy for types containing non-Copy members, we must manually call the .clone() method to duplicate the data.

Types like String, Vec<T>, and Box<T> in Rust store the unsized actual data on the heap and small, fixed-size metadata on the stack. When we assign these types to a new variable, only the stack metadata is copied, the original variable is invalidated, ownership of the heap data is transferred to the new variable, and only the new owner is responsible for freeing the heap memory.

However, when we call .clone() on these types, the heap data is duplicated (deep copy), unless types like Rc<T> and Arc<T>, which are specifically designed to be cloned efficiently by incrementing a reference count on the heap rather than duplicating the actual data in heap.

Move Types in std

  • Heap allocated collections like String, Vec<T>, VecDeque<T>, LinkedList<T>, HashMap<K, V>, HashSet<T>, BTreeMap<K, V>, BTreeSet<T>, BinaryHeap<T>
  • &mut T and most iterators
  • When T, E, P are non-Copy types, then Option<T>, Result<T, E>, Pin<P>, Poll<T>
  • PathBuf, File, TcpStream, TcpListener, JoinHandle, Child processes, channel Sender/Receiver, Mutex<T>, RwLock<T>
  • Smart Pointers like Box<T>, Rc<T>, Arc<T>, Pin<P>
  • Cell<T>, RefCell<T>

More Examples

fn main() {
+    let a = String::from("Hello");
+    let b = a.clone();
+
+    let c = vec![1, 2, 3];
+    let d = c.clone();
+
+    let e: [String; 2] = ["Steve".to_owned(), "Jony".to_owned()];
+    let f = e.clone();
+
+    let g = (128, "Steve".to_string());
+    let h = g.clone();
+
+    println!("{:?} {:?}", a, b); // "Hello" "Hello"
+    println!("{:?} {:?}", c, d); // [1, 2, 3] [1, 2, 3]
+    println!("{:?} {:?}", e, f); // ["Steve", "Jony"] ["Steve", "Jony"]
+    println!("{:?} {:?}", g, h); // (128, "Steve") (128, "Steve")
+}
+
#![allow(unused)]
+
+#[derive(Debug, Clone)]
+struct Person {
+    name: String,
+    age: f32,
+}
+
+fn main() {
+    let a = Person { name: "Bill Gates".to_string(), age: 70.0 };
+    let b = a.clone();
+
+    dbg!(a, b);
+}
+
fn main() {
+    let x: String;
+
+    {
+        let a = String::from("Hello!");
+        x = a; // Ownership is transferred to `x` and `a` is invalidated
+        
+        // At the end of this block, `a` goes out of scope. 
+        // However, no heap data is freed because ownership was moved to `x`
+    }
+
+    println!("{x}"); // "Hello!"
+}
+
\ No newline at end of file diff --git a/docs/docs/ownership/og.jpg b/docs/docs/ownership/og.jpg new file mode 100644 index 00000000..61cc0600 Binary files /dev/null and b/docs/docs/ownership/og.jpg differ diff --git a/docs/docs/panicking/index.html b/docs/docs/panicking/index.html new file mode 100644 index 00000000..5eef29b4 --- /dev/null +++ b/docs/docs/panicking/index.html @@ -0,0 +1,147 @@ +Panicking · Learning Rust

Panicking

panic!()

  • In some cases, when an error occurs we can not do anything to handle it, if the error is something which should not have happened. In other words, if it’s an unrecoverable error.
  • Also when we are not using a feature-rich debugger or proper logs, sometimes we need to debug the code by quitting the program from a specific line of code by printing out a specific message or a value of a variable binding to understand the current flow of the program. +For above cases, we can use panic! macro.

panic!() runs thread based. One thread can be panicked, while other threads are running.

01. Quit from a specific line.

fn main() {
+    // some code
+
+    // if we need to debug in here
+    panic!();
+}
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'explicit panic', src/main.rs:5:5
+

02. Quit with a custom error message.

#[allow(unused_mut)] // 💡 A lint attribute used to suppress the warning; username variable does not need to be mutable
+fn main() {
+    let mut username = String::new();
+
+    // some code to get the name
+  
+    if username.is_empty() {
+        panic!("Username is empty!");
+    }
+
+    println!("{}", username);
+}
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'Username is empty!', src/main.rs:8:9
+

03. Quit with the value of code elements.

#[derive(Debug)] // 💡 A lint attribute which use to implement `std::fmt::Debug` to Color
+struct Color {
+    r: u8,
+    g: u8,
+    b: u8,
+}
+
+#[allow(unreachable_code)] // 💡 A lint attribute used to suppress the warning; unreachable statement
+fn main() {
+    let some_color: Color;
+    
+    // some code to get the color. ex
+    some_color = Color {r: 255, g: 255, b: 0};
+
+    // if we need to debug in here
+    panic!("{:?}", some_color);
+
+    println!(
+        "The color = rgb({},{},{})",
+        some_color.r, some_color.g, some_color.b
+    );
+}
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'Color { r: 255, g: 255, b: 0 }', src/main.rs:16:5
+

As you can see in the above examples panic!() supports println!() type style arguments. By default, it prints the error message, file path and line & column numbers where the error happens.

unimplemented!()

💡 If your code is having unfinished code sections, there is a standardized macro as unimplemented!() to mark those routes. The program will be panicked with a “not yet implemented” error message, if the program runs through those routes.

// error messages with panic!()
+thread 'main' panicked at 'explicit panic', src/main.rs:6:5
+thread 'main' panicked at 'Username is empty!', src/main.rs:9:9
+thread 'main' panicked at 'Color { r: 255, g: 255, b: 0 }', src/main.rs:17:5
+
+// error messages with unimplemented!()
+thread 'main' panicked at 'not yet implemented', src/main.rs:6:5
+thread 'main' panicked at 'not yet implemented: Username is empty!', src/main.rs:9:9
+thread 'main' panicked at 'not yet implemented: Color { r: 255, g: 255, b: 0 }', src/main.rs:17:5
+

unreachable!()

This is the standard macro to mark routes that the program should not enter. The program will be panicked with a “‘internal error: entered unreachable code’” error message, if the program entered those routes.

fn main() {
+    let level = 22;
+    let stage = match level {
+        1..=5 => "beginner",
+        6..=10 => "intermediate",
+        11..=20 => "expert",
+        _ => unreachable!(),
+    };
+    
+    println!("{}", stage);
+}
+
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'internal error: entered unreachable code', src/main.rs:7:20
+

We can set custom error messages for this as well.

// --- with a custom message ---
+_ => unreachable!("Custom message"),
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'internal error: entered unreachable code: Custom message', src/main.rs:7:20
+
+
+// --- with debug data ---
+_ => unreachable!("level is {}", level),
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'internal error: entered unreachable code: level is 22', src/main.rs:7:14
+

assert!(), assert_eq!(), assert_ne!()

These are standard macros which usually use with test assertions.

  • assert!() ensures that a boolean expression is true. It panics if the expression is false.
fn main() {
+    let f = false;
+    
+    assert!(f)
+}
+
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'assertion failed: f', src/main.rs:4:5
+
  • assert_eq!() ensures that two expressions are equal. It panics if the expressions are not equal.
fn main() {
+    let a = 10;
+    let b = 20;
+    
+    assert_eq!(a, b);
+}
+
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'assertion failed: `(left == right)`
+  left: `10`,
+ right: `20`', src/main.rs:5:5
+
  • assert_ne!() ensures that two expressions are not equal. It panics if the expressions are equal.
fn main() {
+    let a = 10;
+    let b = 10;
+    
+    assert_ne!(a, b);
+}
+
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'assertion failed: `(left != right)`
+  left: `10`,
+ right: `10`', src/main.rs:5:5
+

⭐ Expressions which use with assert_eq!() and assert_ne!() should return same data type.

We can set custom error messages for these macros as well. For examples,

  1. With a custom message for assert_eq!()
fn main() {
+    let a = 10;
+    let b = 20;
+    
+    assert_eq!(a, b, "a and b should be equal");
+}
+
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'assertion failed: `(left == right)`
+  left: `10`,
+ right: `20`: a and b should be equal', src/main.rs:5:5
+
  1. assert_eq!() with debug data
fn main() {
+    let a = 10;
+    let b = 20;
+
+    let c = 40;
+    
+    assert_eq!(a+b, c, "a = {} ; b = {}", a, b);
+}
+
+// -------------- Compile-time error --------------
+thread 'main' panicked at 'assertion failed: `(left == right)`
+  left: `30`,
+ right: `40`: a = 10 ; b = 20', src/main.rs:7:5
+

debug_assert!(), debug_assert_eq!(), debug_assert_ne!()

🔎 These are similar to above assert macros. But these statements are only enabled in non optimized builds by default. All these debug_assert macros will be omitted in release builds, unless we pass -C debug-assertions to the compiler.

\ No newline at end of file diff --git a/docs/docs/panicking/og.jpg b/docs/docs/panicking/og.jpg new file mode 100644 index 00000000..58e4b3c6 Binary files /dev/null and b/docs/docs/panicking/og.jpg differ diff --git a/docs/docs/primitive-data-types/index.html b/docs/docs/primitive-data-types/index.html new file mode 100644 index 00000000..31366af4 --- /dev/null +++ b/docs/docs/primitive-data-types/index.html @@ -0,0 +1,125 @@ +Primitive Data Types · Learning Rust

Primitive Data Types

bool

true or false

let a = true;
+let b: bool = false;
+
+// ⭐️ no TRUE, FALSE, 1, 0
+

bool is a single byte(8 bits) in size.

char

A single Unicode scalar value

let a = 'x';
+let b: char = '😎';
+
+// ⭐️ no "x", only single quotes
+

Because of Unicode support, char is not a single byte, but four(32 bits).

i8, i16, i32, i64, i128

8, 16, 32, 64 and 128 bit fixed sized signed(+/-) integer types

DATA TYPEMINMAX
i8-128127
i16-3276832767
i32-21474836482147483647
i64-92233720368547758089223372036854775807
i128-170141183460469231731687303715884105728170141183460469231731687303715884105727

💡 The min and max values are based on the following equation; from -(2ⁿ⁻¹) to 2ⁿ⁻¹-1. You can use MIN and MAX constants to find min and max of each integer type. ex.i8::MIN;

let a = 10; // ⭐️ The default integer type in Rust is i32
+let b: i8 = -128;
+

u8, u16, u32, u64, u128

8, 16, 32, 64 and 128 bit fixed sized unsigned(0/+) integer types

DATA TYPEMINMAX
u80255
u16065535
u3204294967295
u64018446744073709551615
u1280340282366920938463463374607431768211455

💡 The min and max values are based on the following equation; from 0 to 2ⁿ-1. Same way you can use MIN and MAX constants to find min and max of each integer type. ex.u8::MAX

isize, usize

Pointer sized signed and unsigned integer types

The actual bit size depends on the computer architecture you are compiling your program for. By default, the sizes are equal to 32 bits on 32-bit platforms and 64 bits on 64-bit platforms. You can use MIN and MAX constants to find min and max of each integer type. ex.isize::MAX.

🔎 Search more about cross-compiling and supported tiers of Rust programs.

f32, f64

32 and 64 bit sized floating point numbers(numbers with decimal points)

Rust follows IEEE Standard for Binary Floating-Point Arithmetic. The f32 type is similar to float(Single precision) in other languages, while f64 is similar to double(Double precision) in other languages.

let a = 1.5; // ⭐️ The default float type in Rust is f64
+let b: f64 = 2.0;
+

💡 Should avoid using f32, unless you need to reduce memory consumption badly or if you are doing low-level optimization, when targeted hardware does not support for double-precision or when single-precision is faster than double-precision on it.

Array

Fixed size list of elements of same data type

let a = [1, 2, 3];
+
+let b: [i32; 3] = [1, 2, 3]; // with the data type 💡 [Type; NO of elements]
+// 💡let b: [i32; 3] = [1, 2]; // Compiling error : mismatched types : expected an array with a size of 3, found one with a size of 2
+
+let c: [i32; 0] = []; // An empty array
+
// Accessing and changing elements
+let mut a: [i32; 3] = [1, 2, 3];
+a[0] = 2;
+a[1] = 4;
+a[2] = 6;
+
+// Printing with debug and pretty-print debug specifiers
+println!("{a:?}"); // [2, 4, 6]
+println!("{a:#?}");
+//  [
+//      2,
+//      4,
+//      6,
+//  ]
+
let a = [0; 5];   // [0, 0, 0, 0, 0]
+let b = ["x"; 5]; // ["x", "x", "x", "x", "x"]
+

⭐️ Arrays are immutable by default and even with mut, its element count cannot be changed.

🔎 If you are looking for a dynamic/ growable array, you can use vectors. Vectors can contain any type of elements but all elements must be in the same data type.

Tuple

Fixed size ordered list of elements of different(or same) data types

let a = (1, 1.5, true, 'a');
+
+let b: (i32, f64, bool, char) = (1, 1.5, true, 'a'); // With the data types
+
+let c = (0,); // single-element tuple
+
// Accessing and changing elements
+let mut a = (1, 1.5);
+a.0 = 2;
+a.1 = 3.0;
+
+// Printing with debug and pretty-print debug specifiers
+println!("{a:?}"); // (2, 3.0)
+println!("{a:#?}");
+// (
+//   2,
+//   3.0,
+// )
+
// Destructuring
+let a = (1, 1.5);
+let (b, c) = a; // b = 1, c = 1.5
+
+let a = (1, 1.5, true, 'a');
+let (b, _, _, c) = a; // b = 1, c = 'a'
+let (b, .., c) = a; // b = 1, c = 'a'
+
+let a = (1, 2, 3, true, false, 'a', 'b', 'c');
+let (b, ..) = a; // b = 1
+let (.., c) = a; // c = 'c'
+let (e, f, g, .., h, i, j) = a; // 1 2 3 'a' 'b' 'c'
+
// Nesting
+let a = (1, 1.5);
+let b = (a, (2, 4), 6); // ((1, 1.5), (2, 4), 6)
+

⭐️ Tuples are also immutable by default and even with mut, its element count cannot be changed. Also, if you want to change an element’s value, the new value should have the same data type of previous value.

Slice

Dynamically-sized reference to another data structure

Imagine you want to get/ pass a part of an array or any other data structure. Instead of copying it to another array (or same data structure), Rust allows for creating a view/ reference to access only that part of the data. This view/ reference can be mutable or immutable.

let a: [i32; 4] = [1, 2, 3, 4]; // Parent Array
+
+// Slice whole array
+let b: &[i32] = &a; // data type is optional
+let c = &a[0..4]; // From 0th position to 4th(excluding)
+let d = &a[..]; // high or low bounds are optional
+
+// Slicing part of the array
+let e = &a[1..3]; // [2, 3]
+let f = &a[1..]; // [2, 3, 4]
+let g = &a[..3]; // [1, 2, 3]
+
// directly creating a slice
+let h = &[1, 2];
+let h: &[i32] = &[1, 2]; // with the datatype
+

str

Unsized UTF-8 sequence of Unicode string slices

let a = "Hello, world."; // a: &'static str
+let b: &str = "こんにちは, 世界!";
+

str is a UTF-8 sequence of Unicode string slice; It’s unsized. You can’t create a variable of type str directly because Rust needs to know sizes at compile time. So, it must always be used behind a pointer — like &str, Box<str>, or Rc<str>.

&str is a fat pointer (a pointer to str and a length). So, it’s a sized reference that carries both a pointer and a length.

&'static str is an &str that is statically allocated directly to the read-only data segment in the program binary. The 'static lifetime means it lives for the entire program duration.

💯 String

  • 💡 String: capital “S” as it’s a struct.
  • String type is a growable, heap-allocated, UTF-8 encoded string. It is a sized type.
  • String is the most common string type. In general, you should use String when you need ownership, and &str when you just need to borrow a string.
  • Can be generated from a &str type, via to_string(), to_owned() or String::from() methods. With as_str() method, a String type can be converted to a &str type.
let s: &str = "Hello"; // &str
+
+let a = s.to_string(); // String
+let b = s.to_owned(); // String
+let c = String::from(s); // String
+
let a = String::from("Hello"); // String
+let b = a.as_str(); // &str
+

Function

p1 is a function pointer to plus_one() in the following code.

fn main() {
+    let p1 = plus_one; // Without type declarations
+    let a = p1(5); // 6
+
+    let p1: fn(i32) -> i32 = plus_one; // With the type declarations
+    let b = p1(5); // 6
+}
+
+fn plus_one(i: i32) -> i32 {
+    i + 1
+}
+

Type Inference

The Rust compiler can infer the data type when we don’t explicitly specify it. For example, in Rust, the default integer type is i32 and the default float type is f64. However, the Rust compiler checks the usage and determines the best data type based on the context.

let a = 10;   // type inference: i32
+let b = 3.14; // type inference: f64
+let c = true; // type inference: bool
+
fn main() {
+    let a = 8; // type inference: i8
+    println!("{}", plus_one(a));
+}
+
+fn plus_one(i: i8) -> i8 {
+    i + 1
+}
+

Default Byte Sizes of Data Types

We can check the default byte sizes of data types via std::mem::size_of. For example: size_of::<bool>(), size_of::<f32>(), size_of::<&str>(), etc.

Data typebytesbits
bool18
char432
i8 i16 i32 i64 i1281, 2, 4, 8, 168, 16, 32, 64, 128
u8 u16 u32 u64 u1281, 2, 4, 8, 168, 16, 32, 64, 128
isize usize8, 864, 64
f32 f644, 832, 64
&str16128

👨‍🏫 Before going to the next…

  • Other than adding the type annotations to the variables, for numeric types, we can append the data type directly to the value as the suffix. Also, to improve the readability of long numbers, we can use _ as a divider.

    let a = 5i8; // Equals to `let a: i8 = 5;`
    +
    +let b = 100_000_000; // Equals to `let b = 100000000;`
    +// 💡 The placements of _s are not strict. ex. 10000_0000 is also valid.
    +
    +let pi = 3.141_592_653_59_f64; // Equals to `let pi: f64 = 3.14159265359`
    +// 💡 make sure group digits consistently by underscores avoid warnings
    +
    +const PI: f64 = 3.141_592_653_59; // In the constants and statics, the data type must be annotated in the beginning.
    +
\ No newline at end of file diff --git a/docs/docs/primitive-data-types/og.jpg b/docs/docs/primitive-data-types/og.jpg new file mode 100644 index 00000000..207ac39d Binary files /dev/null and b/docs/docs/primitive-data-types/og.jpg differ diff --git a/docs/docs/rust_playground.png b/docs/docs/rust_playground.png new file mode 100644 index 00000000..580f0049 Binary files /dev/null and b/docs/docs/rust_playground.png differ diff --git a/docs/docs/smart-compiler/index.html b/docs/docs/smart-compiler/index.html new file mode 100644 index 00000000..27c76acc --- /dev/null +++ b/docs/docs/smart-compiler/index.html @@ -0,0 +1,82 @@ +Smart Compiler · Learning Rust

Smart Compiler

Why Compiler?

The Rust compiler does the most significant job to prevent errors in Rust programs. It analyzes the code at compile-time and issues warnings, if the code does not follow memory management rules or lifetime annotations correctly.

For example,

#[allow(unused_variables)] //💡 A lint attribute used to suppress the warning; unused variable: `b`
+fn main() {
+    let a = vec![1, 2, 3];
+    let b = a;
+
+    println!("{:?}", a);
+}
+
+
+// ------ Compile-time error ------
+error[E0382]: use of moved value: `a`
+ --> src/main.rs:6:22
+  |
+3 |     let b = a;
+  |         - value moved here
+4 |
+5 |     println!("{:?}", a);
+  |                      ^ value used here after move
+  |
+  = note: move occurs because `a` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
+
+error: aborting due to previous error
+For more information about this error, try `rustc --explain E0382`.
+
+// ⭐ instead using #[allow(unused_variables)], consider using "let _b = a;" in line 4. 
+// Also you can use "let _ =" to completely ignore return values
+

💭 In the previous sections, we have discussed memory management concepts like ownership, borrowing, lifetimes and etc.

Rust compiler checks not only issues related with lifetimes or memory management and also common coding mistakes, like the following code.

struct Color {
+    r: u8,
+    g: u8,
+    b: u8,
+}
+
+fn main() {
+    let yellow = Color {
+        r: 255,
+        g: 255,
+        d: 0,
+    };
+
+    println!("Yellow = rgb({},{},{})", yellow.r, yellow.g, yellow.b);
+}
+
+
+// ------------ Compile-time error ------------
+error[E0560]: struct `Color` has no field named `d`
+  --> src/main.rs:11:9
+   |
+11 |         d: 0,
+   |         ^ field does not exist - did you mean `b`?
+
+error: aborting due to previous error
+For more information about this error, try `rustc --explain E0560`.
+

Explain Error Codes

Above error messages are very descriptive and we can easily see where is the error. But while we can not identify the issue via the error message, rustc --explain commands help us to identify the error type and how to solve it, by showing simple code samples which express the same problem and the solution we have to use.

For example, rustc --explain E0571 shows the following output in the console.

A `break` statement with an argument appeared in a non-`loop` loop.
+
+Example of erroneous code:
+```
+let result = while true {
+    if satisfied(i) {
+        break 2*i; // error: `break` with value from a `while` loop
+    }
+    i += 1;
+};
+```
+
+The `break` statement can take an argument (which will be the value of the loop
+expression if the `break` statement is executed) in `loop` loops, but not
+`for`, `while`, or `while let` loops.
+
+Make sure `break value;` statements only occur in `loop` loops:
+```
+let result = loop { // ok!
+    if satisfied(i) {
+        break 2*i;
+    }
+    i += 1;
+};
+```
+

💡 Also you can read the same explanations via Rust Compiler Error Index. For example to check the explanation of E0571 error, you can use https://doc.rust-lang.org/error-index.html#E0571.

\ No newline at end of file diff --git a/docs/docs/smart-compiler/og.jpg b/docs/docs/smart-compiler/og.jpg new file mode 100644 index 00000000..9e1bfa0a Binary files /dev/null and b/docs/docs/smart-compiler/og.jpg differ diff --git a/docs/docs/std-primitives-and-preludes/index.html b/docs/docs/std-primitives-and-preludes/index.html new file mode 100644 index 00000000..8c312d6c --- /dev/null +++ b/docs/docs/std-primitives-and-preludes/index.html @@ -0,0 +1,93 @@ +STD, Primitives and Preludes · Learning Rust

STD, Primitives and Preludes

⭐️ In Rust, language elements are implemented by not only std library crate but also compiler as well. Examples,

  • Primitives: Defined by the compiler and methods are implemented by std library directly on primitives.
  • Standard Macros: Defined by both compiler and std

The std library has been divided into modules, according to the main areas each covered.

⭐️ While primitives are implemented by the compiler, the standard library implements the most useful methods directly on the primitive types. But some rarely useful language elements of some primitives are stored on relevant std modules. This is why you can see char, str and integer types on both primitives and std modules.

Primitives

// Primitives: Defined by the compiler and methods are directly implemented by std
+bool, char, slice, str
+
+i8, i16, i32, i64, i128, isize
+u8, u16, u32, u64, u128, usize
+
+f32, f64
+
+array, tuple
+
+pointer, fn, reference
+

Standard Macros

// Standard Macros also defined by both compiler and std
+print, println, eprint, eprintln
+format, format_args
+write, writeln
+
+concat, concat_idents, stringify // concat_idents: nightly-only experimental API
+
+include, include_bytes, include_str
+
+assert, assert_eq, assert_ne
+debug_assert, debug_assert_eq, debug_assert_ne
+
+try, panic, compile_error, unreachable, unimplemented
+
+file, line, column, module_path
+env, option_env
+cfg
+
+select, thread_local // select: nightly-only experimental API
+
+vec
+

Std Modules

// std modules
+char, str
+
+i8, i16, i32, i64, i128, isize
+u8, u16, u32 ,u64, u128, usize
+f32, f64
+num
+
+vec, slice, hash, heap, collections // heap: nightly-only experimental API
+
+string, ascii, fmt
+
+default
+
+marker, clone, convert, cmp, iter
+
+ops, ffi
+
+option, result, panic, error
+
+io
+fs, path
+mem, thread, sync
+process, env
+net
+time
+os
+
+ptr, boxed, borrow, cell, any, rc
+
+prelude
+
+intrinsics // intrinsics: nightly-only experimental API
+raw // raw: nightly-only experimental API
+

🔎 When examining Rust’s source code, you can see that the src directory is a workspace. Even though it is having many library crates, by examining root Cargo.toml file you can easily identify that main crates are rustc(compiler) and libstd (std). In libstd/lib.rs std modules have been re-exported via pub use and the original location of most of the std modules is src/libcore.

Few important std modules are,

  • std::io - Core I/O functionality
  • std::fs - Filesystem specific functionality
  • std::path - Cross-platform path specific functionality
  • std::env - Process’s environment related functionality
  • std::mem - Memory related functionality
  • std::net - TCP/UDP communication
  • std::os - OS specific functionality
  • std::thread - Native threads specific functionality
  • std::collections - Core Collection types

💯 Refer Rust Standard Library Documentation for more details.

Preludes

Even though Rust std contains many modules, by default it doesn’t load each and everything of std library on every rust program. Instead, it loads only the smallest list of things which require for almost every single Rust program. These are called preludes. They import only,

// Reexported core operators
+pub use marker::{Copy, Send, Sized, Sync};
+pub use ops::{Drop, Fn, FnMut, FnOnce};
+
+// Reexported functions
+pub use mem::drop;
+
+// Reexported types and traits
+pub use boxed::Box;
+pub use borrow::ToOwned;
+pub use clone::Clone;
+pub use cmp::{PartialEq, PartialOrd, Eq, Ord};
+pub use convert::{AsRef, AsMut, Into, From};
+pub use default::Default;
+pub use iter::{Iterator, Extend, IntoIterator};
+pub use iter::{DoubleEndedIterator, ExactSizeIterator};
+pub use option::Option::{self, Some, None};
+pub use result::Result::{self, Ok, Err};
+pub use slice::SliceConcatExt;
+pub use string::{String, ToString};
+pub use vec::Vec;
+

Preludes have been imported explicitly on libstd/lib.rs and the whole list can be seen on libstd/prelude/v1.rs.

⭐️ So technically, Rust inserts,

  • extern crate std; : into the crate root of every crate
  • use std::prelude::v1::*; : into every module +So you don’t need to import these each time.

The concept of preludes is quite common on Rust libraries. Even some modules inside std crate (ex.std::io) and many libraries (ex. Diesel) are having their own prelude modules.

⭐️ But preludes are used to create a single place to import all important components which are required while using the library. They do not load automatically unless you imported them manually. Only std::prelude imports automatically in every Rust programs.

\ No newline at end of file diff --git a/docs/docs/std-primitives-and-preludes/og.jpg b/docs/docs/std-primitives-and-preludes/og.jpg new file mode 100644 index 00000000..bf8fb014 Binary files /dev/null and b/docs/docs/std-primitives-and-preludes/og.jpg differ diff --git a/docs/docs/structs/index.html b/docs/docs/structs/index.html new file mode 100644 index 00000000..62529418 --- /dev/null +++ b/docs/docs/structs/index.html @@ -0,0 +1,157 @@ +Structs · Learning Rust

Structs

  • Used to encapsulate related properties into one unified data type.
  • By convention, the name should follow PascalCase.
  • 3 variants,
    • C-like structs: One or more , separated name: value pairs enclosed in {}

      struct Color {
      +    red: u8,
      +    green: u8,
      +    blue: u8,
      +}
      +
    • Tuple structs: One or more , separated values enclosed in ()

      struct Color(u8, u8, u8);
      +
    • Unit structs: A struct with no fields/ members

      struct Black;
      +

⭐️ In Rust, data (attributes) and behavior (associated functions and methods) are placed separately. Structs and Enums are used to group related data, and impls and traits are used to add associated and shared behavior to that data.

💡 In Rust, the term “instantiation” is used to describe the act of creating a concrete instance of a type (struct or enum).

💡 In Rust, the term “field” is used to describe a named component in a C-like struct & struct-like enum variant, and the term “element” is used to describe an unnamed component in a tuple struct & tuple-like enum variant. The term “member” is used to describe both.

💯 More complex examples can be found on Impls and Traits, Lifetimes and Modules sections.

C-like Structs

  • Similar to classes (without its methods) in OOP languages.
  • Can access fields using the ./ dot notation and the field name.

Definition

struct Color {
+    red: u8,
+    green: u8,
+    blue: u8,
+}
+

Instantiation & Accessing Fields

struct Color {
+    red: u8,
+    green: u8,
+    blue: u8,
+}
+
+fn main() {
+    // 1. Instantiation
+    let white = Color {
+        red: 255,
+        green: 255,
+        blue: 255,
+    };
+
+    // 2. Instantiation without redundant field names, when using the same variable names
+    let (red, green, blue) = (0, 0, 0);
+    let black = Color { red, green, blue };
+
+    // 3. Instantiation + copy fields' values from another instance
+    let red = Color { red: 255, .. black }; // 💡 Copy green and blue from black
+    let green = Color { green: 255, .. black }; // 💡 Copy red and blue from black
+    let mut blue = Color { .. black }; // 💡 Copy all fields' values from black
+    blue.blue = 255;
+
+     println!("RGB({}, {}, {})", white.red, white.green, white.blue); // RGB(255, 255, 255)
+     println!("RGB({}, {}, {})", black.red, black.green, black.blue); // RGB(0, 0, 0)
+
+     println!("RGB({}, {}, {})", red.red, red.green, red.blue); // RGB(255, 0, 0)
+     println!("RGB({}, {}, {})", green.red, green.green, green.blue); // RGB(0, 255, 0)
+     println!("RGB({}, {}, {})", blue.red, blue.green, blue.blue); // RGB(0, 0, 255)
+}
+
// 4. Instantiation with default values
+
+#[derive(Default)]
+struct Person {
+    name: String,
+    age: f32,
+}
+
+fn main() {
+    let a = Person::default(); // Instantiation with default values
+
+    assert_eq!(a.name, ""); // String default value ""
+    assert_eq!(a.age, 0.0); // f32 default value 0.0
+}
+

💡 In Rust, the #[derive()] attribute is used to automatically generate an implementation of certain traits for a custom data structure (struct and enum), instead of you writing them by hand. The std::default::Default trait allows us to create a new instance of a type with the Type::default() method.

💯 5. We can also use a constructor function inside an impl block to initialize a struct.

Destructuring

struct Person {
+    name: String,
+    company_name: String,
+}
+
+fn get_steve() -> Person {
+    Person {
+        name: "Steve Jobs".to_string(),
+        company_name: "Apple".to_string(),
+    }
+}
+
+fn main() {
+    let steve = Person {
+        name: "Steve Jobs".to_string(),
+        company_name: "Apple".to_string(),
+    };
+
+    let Person {name: a, company_name: b} = steve; // 1. Destructuring fields' values to a and b
+    println!("{a} {b}"); // Steve Jobs Apple
+
+    let Person {company_name: c, .. } = get_steve(); // 2. Destructuring only selected fields' values; directly from the function call
+    println!("{c}"); // Apple
+}
+
+// 💯 let Person {name: ref a, company_name: ref b} = steve; // add ref keyword, to pass a field's value as a reference
+

Tuple Structs

  • Looks like a named tuples.
  • Can access fields using the ./ dot notation and the index number of the field, like on tuples.
  • ⭐️ When a tuple struct has only one element, we call it newtype pattern. Because it helps to create a new type.

Definition

struct Color(u8, u8, u8);
+
+struct Department(String);
+

Instantiation & Accessing Elements

struct Color(u8, u8, u8);
+
+struct Department(String);
+
+fn main() {
+    let white = Color(255, 255, 255);
+    println!("RGB({}, {}, {})", white.0, white.1, white.2); // RGB(255, 255, 255)
+
+    let eng_department = Department("Engineering".to_string());
+    println!("{}", eng_department.0); // Engineering
+}
+

Destructuring

struct Color(u8, u8, u8);
+
+struct Department(String);
+
+fn get_department() -> Department {
+    Department("Engineering".to_string())
+}
+
+fn main() {
+    let white = Color(255, 255, 255);
+
+    let Color(red, green, blue) = white; // 💡 let Color(red, blue, .. ) = white; // Destructuring only selected field's value
+    println!("RGB({}, {}, {})", red, green, blue); // RGB(255, 255, 255)
+
+    let Department(name) = get_department();
+    println!("{}", name); // Engineering
+}
+

Unit Structs

  • It defines a new type, but it resembles an empty tuple, ()
  • This is rarely useful on its own. But in combination with other features (such as generics), it can become useful.

Definition & Instantiation

struct Electron;
+
+fn main() {
+    let x = Electron;
+}
+

📖 ex: A library may ask you to create a structure that implements a certain trait to handle events. If you don’t have any data you need to store in the structure, you can create a unit-like struct.

Debug Printing and Pretty Debug Printing

In Rust, the #[derive()] attribute is used to automatically generate an implementation of certain traits for a custom data structure (struct and enum), instead of you writing them by hand. The std::fmt::Debug trait allows us to format a value with {:?} or {:#?} in println! and similar macros.

#![allow(unused)] // 💡 skip unused warnings, as we don't read fields in the structs
+
+#[derive(Debug)]
+struct Electron;
+
+#[derive(Debug)]
+struct Department(String);
+
+#[derive(Debug)]
+struct Person {
+    name: String,
+    company_name: String,
+}
+
+fn main() {
+    let a = Electron;
+    println!("{a:?}"); // Electron // 💡{a:#?} prints the same
+
+    let b = Department("Engineering".to_string());
+    println!("{b:?}"); // Department("Engineering")
+    println!("{b:#?}");
+    // Department(
+    //     "Engineering",
+    // )
+    
+    let c = Person { name: "Steve Jobs".to_string(), company_name: "Apple".to_string() };
+    println!("{c:?}"); // Person { name: "Steve Jobs", company_name: "Apple" }
+    println!("{c:#?}");
+    // Person {
+    //     name: "Steve Jobs",
+    //     company_name: "Apple",
+    // }
+}
+
\ No newline at end of file diff --git a/docs/docs/structs/og.jpg b/docs/docs/structs/og.jpg new file mode 100644 index 00000000..6eabb228 Binary files /dev/null and b/docs/docs/structs/og.jpg differ diff --git a/docs/docs/traits/index.html b/docs/docs/traits/index.html new file mode 100644 index 00000000..56614d88 --- /dev/null +++ b/docs/docs/traits/index.html @@ -0,0 +1,436 @@ +Traits · Learning Rust

Traits

A trait is a contract that defines a set of behaviors or properties that a type must implement. It can contain associated types, constants, function or method signatures, and overridable default implementations.

Definition

With No Associates

💡 Mostly used to mark a type as having certain properties to allow in certain operations. Known as Marker Traits.

pub trait Sized { }
+

With the Declarations of Associates

trait Greet {
+    const PREFIX: &'static str;
+    type Item;
+    fn greet(&self) -> String;
+}
+

With the Default Implementations of Associates

trait Greet {
+    const PREFIX: &'static str = "Hello";
+    
+    fn greet(&self) -> String {
+        format!("{}!", String::from(Self::PREFIX))
+    }
+}
+

With Supertraits

A trait must have to be implemented first before implementing the current trait.

pub trait Copy: Clone { } // 💡 Any type that copyable should be clonable
+
+// 💡 Any type that clonable should be sized
+pub trait Clone: Sized {
+    fn clone(&self) -> Self;
+    fn clone_from(&mut self, source: &Self) { ... }
+}
+

💯 trait Subtrait: Super or trait Subtrait: SupertraitA + SupertraitB

With Generic Types

// 💡 Convert from one type to another. If successful, retrun Type; else return associated Error
+pub trait TryFrom<T>: Sized {
+    type Error;
+
+    fn try_from(value: T) -> Result<Self, Self::Error>;
+}
+

Trait Impls (Manual Implementation)

Manually implementing a shared behavior defined in a trait for a type via an impl block.

With Required Components

When a trait has only declarations of associated items, it’s required to implement all of them.

struct Person {
+    name: String
+}
+
+trait Greet {
+    const PREFIX: &'static str; // 💡 Required constant
+    fn greet(&self) -> String; // 💡 Required method
+}
+
+impl Greet for Person {
+    const PREFIX: &'static str = "Hello";
+    
+    fn greet(&self) -> String {
+        format!("{} {}!", Self::PREFIX.to_owned(), self.name)
+    }
+}
+
+fn main() {
+    let steve = Person { name: "Steve".to_string() };
+    println!("{}", steve.greet()); // Hello Steve!
+}
+

With Provided Components

When a trait has default values and default implementations, it’s possible to implement only some of them or override them.

struct Person {
+    name: String
+}
+
+trait Greet {
+    // 💡 Provided constant
+    const PREFIX: &'static str = "Hello";
+
+    // 💡 Provided method
+    fn greet(&self) -> String {
+        format!("{}!", String::from(Self::PREFIX))
+    }
+}
+
+impl Greet for Person {
+    // 💡 Overridden constant
+    const PREFIX: &'static str = "Good morning";
+    
+    // 💡 Overridden method
+    fn greet(&self) -> String {
+        format!("{} {}!", Self::PREFIX.to_owned(), self.name)
+    }
+}
+
+fn main() {
+    let steve = Person { name: "Steve".to_string() };
+    println!("{}", steve.greet()); // Good morning Steve!
+}
+

For Enum Types

#![allow(unused)]
+enum Shape {
+    Circle(f64),         // radius
+    Rectangle(u32, u32), // width, height
+}
+
+trait Area {
+    fn area(&self) -> f64;
+}
+
+impl Area for Shape {
+    fn area(&self) -> f64 {
+        match *self {
+            Shape::Circle(radius) => std::f64::consts::PI * radius * radius,
+            Shape::Rectangle(width, height) => (width * height) as f64,
+        }
+    }
+}
+
+fn main() {
+    let circle = Shape::Circle(7.0);
+    let rect = Shape::Rectangle(5, 5);
+
+    println!("{:?}", circle.area());
+    println!("{:?}", rect.area());
+}
+

For Generic Types

  1. impl<T> Trait for Type<T>
#![allow(unused)]
+struct Person<T> {
+    name: String,
+    age: T,
+}
+
+trait Greet {
+    fn greet(&self) -> String;
+}
+
+impl<T> Greet for Person<T> {
+    fn greet(&self) -> String {
+        format!("Hello {}!", self.name)
+    }
+}
+
+fn main() {
+    let steve = Person { name: "Steve".to_string(), age: 65 }; // 💡 age: i32
+    let bill = Person { name: "Bill".to_string(), age: 7.5 }; // 💡 age: f64
+
+    println!("{:?}", steve.greet()); // Hello Steve!
+    println!("{:?}", bill.greet()); // Hello Bill!
+}
+
  1. impl<T> Trait<T> for Type<T>
struct Point<T> {
+    x: T,
+    y: T,
+}
+
+trait IntoTuple<T> {
+    fn into_tuple(self) -> (T, T);
+}
+
+impl<T> IntoTuple<T> for Point<T> {
+    fn into_tuple(self) -> (T, T) {
+        (self.x, self.y)
+    }
+}
+
+fn main() {
+    let a = Point { x: 0, y: 1 }; // Point<i32>
+    let b = Point { x: "2.0", y: "2.2" }; // Point<&str>
+
+    println!("{:?}", a.into_tuple()); // (0, 1)
+    println!("{:?}", b.into_tuple()); // ("2.0", "2.2")
+}
+

👨‍🏫 Update the above implementation to impl<T, U> Trait<T> for Type<U>.

Derive and Auto Traits (Autogenerate Implementations)

Derive Traits

The compiler generates the trait implementation automatically for you, based on the derived attributes.

#[derive(Debug, Clone, PartialEq)]
+struct Person {
+    name: String,
+}
+
+fn main() {
+    let steve = Person { name: "Steve".to_string() };
+    let bill = Person { name: "Bill".to_string() };
+
+    // Debug: 💡 allow debug print with {:?}
+    println!("{steve:?}"); // Person { name: "Steve" }
+
+    // Clone: 💡 allow duplicate data via .clone()
+    let gates = bill.clone();
+
+    // PartialEq: 💡 support equate the two instances via == and !=
+    println!("{}", steve == bill); // false
+    println!("{}", bill == gates); // true
+}
+

🔎 Check bellow Rust STD documentation pages, of these normal derive-traits.

TraitThe functionality (implement to the type at compile-time)
DebugEnables debug-printing of the internal state with {:?}
DefaultAllows creating a default initial value with ::default()
CloneAllows creating a deep copy explicitly with .clone()
PartialEq / EqAllows comparing instances with == and != operators
PartialOrd / OrdAllows comparing instances with <, <=, >, and >= operators
HashAllows the type to be used as a key in a HashMap or HashSet

💯 Marker traits are the traits that have no associated constants, types, methods, etc. So, the compiler-generated implementation has only an empty impl Trait for Type { } block.

#![allow(unused)]
+
+#[derive(Debug, Clone, Copy)] // 💡 Copy trait needs the Clone trait as a supertrait
+struct Point {
+    x: i32,
+    y: i32,
+}
+
+fn main() {
+    let a = Point { x: 0, y: 1 };
+    let b = a; // Copy: 💡 duplicate data. a and b are separate instances
+
+    println!("{a:?}");
+    println!("{b:?}");
+}
+// 👨‍🏫 Try remove Copy derive on Point and explisitly copy via .clone()
+

🔎 Copy set the ability to duplicate via simply copying bits (pass by duplicating)

Auto Traits

Implicitly bound/ automatically implemented by the compiler without any keywords.

pub trait Sized { }
+pub unsafe auto trait Sync { }
+pub auto trait Unpin { }
+
#![allow(unused)]
+
+#[derive(Debug)]
+struct Point { // 💡 Sized, Send, Sync: (Automatic) Implicitly bound for most simple types in Rust
+    x: i32,
+    y: i32,
+}
+
+fn main() {
+    let a = Point { x: 0, y: 1 };
+
+    std::thread::scope(|s| {
+        s.spawn(|| println!("{a:?}"));
+        s.spawn(|| println!("{a:?}"));
+    });
+
+    std::thread::spawn(move || println!("{a:?}")).join().unwrap();
+}
+
TraitThe properties (set to the type automatically)
SizedAble to determine the size at compile time
UnpinSafe to move in memory
SendSafe to move ownership across threads
SyncSafe to shared references between threads
UnwindSafeSafe to use in panic unwindings
RefUnwindSafeSafe to use shared references in panic unwinding

👨‍🏫 Sized/ Unsized

  • ⭐️ Most simple types in Rust have a fixed size known at compile time and automatically implement Sized marker trait.
  • 💯 Unsized types (or dynamically sized types, DSTs) cannot be used directly as local variables, function parameters, or return values. They must always be placed behind an indirection, such as a reference (& or &mut), a Box, Rc, or Arc. Common examples include slices ([T], &str) and trait objects (dyn Trait).
    • 🔎 Pointers to these types are known as “wide pointers” or “fat pointers” because they contain both a pointer to the data and additional metadata. Metadata: for slices - length (number of elements) and for trait objects - a pointer to the vtable/ virtual function table. Ex: &[T], *mut str, Box<dyn Trait>
  • 💡 To allow a type to be unsized (relaxes the default Sized requirement), we can use the ?Sized bound.

Static Dispatch

Monomorphization is a compiler optimization and implementation strategy that transforms polymorphic (generic) code into specialized, monomorphic (single-type) versions for each unique set of concrete types used in a program. This increases the binary size.

Dispatch is the process of deciding which implementation of a polymorphic function to execute, either statically at compile time or dynamically at runtime (via a lookup in a vtable).

Rust compiler generates highly optimized code blocks for each type used for a generic function. So, function calls are statically resolved at compile time and no runtime overhead.

Trait Bounds

Specify the type constraints by traits in generics (<T: Trait>) rather than using exact types. Allowed in a field types & function return and argument.

struct Point { x: i32, y: i32 }
+impl Addr for Point {
+    fn addr(&self) -> String {
+        format!("{}, {}", self.x, self.y)
+    }
+}
+
+struct MapLocation(String, String); // latitude, longitude
+impl Addr for MapLocation {
+    fn addr(&self) -> String {
+        format!("{}, {}", self.0, self.1)
+    }
+}
+
+// --- ⭐️ main Trait & Type with Trait Bounds ⭐️
+trait Addr {
+    fn addr(&self) -> String;
+}
+
+struct Delivery<T: Addr> {
+    location: T,
+}
+
+impl<T: Addr> Delivery<T> {
+    fn new(location: T) -> Self {
+        Self { location }
+    }
+}
+
+// --- ⭐️ fn with Trait Bounds ⭐️
+fn location_info<T: Addr>(location: T) {
+    println!("Latitude/ Longitude: {}", location.addr())
+}
+
+// ---
+fn main() {
+    let ocean = Point { x: 35, y: 20 };
+    let tokyo = MapLocation("35.68951".to_string(), "139.69170".to_string());
+
+    let pkg1 = Delivery::new(ocean);
+    let pkg2 = Delivery::new(tokyo);
+
+    location_info(pkg1.location); // Latitude/ Longitude: 35, 20
+    location_info(pkg2.location); // Latitude/ Longitude: 35.68951, 139.69170
+}
+

Opaque Types

Specify the type constraints via traits (:impl Trait) without specifying full generic syntax. But only allowed as a function return or an argument.

struct Point { x: i32, y: i32 }
+impl Addr for Point {
+    fn addr(&self) -> String {
+        format!("{}, {}", self.x, self.y)
+    }
+}
+
+struct MapLocation(String, String); // latitude, longitude
+impl Addr for MapLocation {
+    fn addr(&self) -> String {
+        format!("{}, {}", self.0, self.1)
+    }
+}
+
+trait Addr {
+    fn addr(&self) -> String;
+}
+
+// --- ⭐️ Opaque Types are only allowed in function parameters 
+fn location_info(location: impl Addr) { // 💡 Argument-Position-Impl-Trait/ APIT
+    println!("Latitude/ Longitude: {}", location.addr())
+}
+
+fn tokyo_location() -> impl Addr { // 💡 Return-Position-Impl-Trait/ RPIT
+    MapLocation("35.68951".to_string(), "139.69170".to_string())
+}
+
+// ---
+fn main() {
+    let ocean = Point { x: 35, y: 20 };
+    let tokyo = tokyo_location();
+
+    location_info(ocean); // Latitude/ Longitude: 35, 20
+    location_info(tokyo); // Latitude/ Longitude: 35.68951, 139.69170
+}
+

Dynamic Dispatch

Dynamic dispatch is used when we need to handle multiple different concrete types (mix types) and resolve the trait implementation dynamically at runtime (because this is not possible with static dispatch).

🔎 The compiler uses vtable (virtual method table) pointers. Instead of knowing the type at compile time, the program follows a pointer to a table that contains the addresses of the methods for that specific instance.

Trait Objects

struct Point { x: i32, y: i32 }
+impl Addr for Point {
+    fn addr(&self) -> String {
+            format!("{}, {}", self.x, self.y)
+    }
+}
+
+struct MapLocation(String, String); // latitude, longitude
+impl Addr for MapLocation {
+    fn addr(&self) -> String {
+        format!("{}, {}", self.0, self.1)
+    }
+}
+
+trait Addr {
+    fn addr(&self) -> String;
+}
+
+// --- ⭐️ Trait Object, allow mix types and check dynamically at runtime
+fn locations_info(locations: &[Box<dyn Addr>]) { // 💡 `dyn Addr` is unsized (DST) and needs a pointer
+    for location in locations {
+        println!("Latitude/ Longitude: {}", location.addr())
+    }
+}
+
+// ---
+fn main() {
+    let ocean = Point { x: 35, y: 20 };
+    let tokyo = MapLocation("35.68951".to_string(), "139.69170".to_string());
+
+    let locations: Vec<Box<dyn Addr>> = vec![Box::new(ocean), Box::new(tokyo)];
+
+    locations_info(&locations);
+    // Latitude/ Longitude: 35, 20
+    // Latitude/ Longitude: 35.68951, 139.69170
+}
+

Blanket Impls (One-to-Many Manual Implementation)

Blanket impls are a special kind of trait implementation that applies to all types that have a certain type constraint/bound.

A Simple Blanket Impl

#![allow(unused)]
+
+// --- 💡 Two types with Greet implentation
+struct Person { name: String }
+impl Greet for Person {}
+
+struct Stranger {}
+impl Greet for Stranger {}
+
+trait Greet {
+    const PREFIX: &'static str = "Hello";
+
+    fn greet(&self) -> String {
+        format!("{}!", String::from(Self::PREFIX))
+    }
+}
+
+// --- New Farewell trait
+trait Farewell {
+    const PREFIX: &'static str = "Goodbye";
+
+    fn farewell(&self) -> String {
+        format!("{}!", String::from(Self::PREFIX))
+    }
+}
+
+// ⭐️ Any type that implements Greet gets this Farewell implementation
+impl<T: Greet> Farewell for T {} // 💡or impl<T> Farewell for T where T: Greet {}
+
+// ---
+fn main() {
+    let steve = Person { name: "Steve".to_string() };
+    let stranger = Stranger {};
+
+    println!("{}", steve.greet()); // Hello!
+    println!("{}", stranger.greet()); // Hello!
+
+    println!("{}", steve.farewell()); // Goodbye!
+    println!("{}", stranger.farewell()); // Goodbye!
+}
+

Useful Blanket Impls in STD

  1. When implement Display Rust STD automatically implements ToString to the type.
use std::fmt;
+
+struct Person {
+    name: String,
+}
+
+impl fmt::Display for Person {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "Person {{ name: {}}}", self.name) // 💡 {{ }} escaping curly braces
+    }
+}
+
+fn main() {
+    let steve = Person {
+        name: "Steve".to_string(),
+    };
+
+    println!("{steve}"); // Person { name: Steve}
+    println!("{}", steve.to_string()); // Person { name: Steve}
+}
+
  1. When implement From<T> Rust STD automatically implements Into<U> to the type.
#![allow(unused)]
+
+use std::convert::From;
+
+#[derive(Debug)]
+struct Person {
+    name: String,
+}
+
+impl From<&str> for Person {
+    fn from(name: &str) -> Self {
+        Person {
+            name: name.to_string(),
+        }
+    }
+}
+
+fn main() {
+    let steve = Person::from("Steve");
+    let bill: Person = "Bill".into();
+
+    println!("{:?}", steve); // Person { name: "Steve" }
+    println!("{:?}", bill); // Person { name: "Bill" }
+}
+

👨‍🏫 Try to implement TryFrom and check the blanket implementation it provides.

Trait Coherence & Orphan Rule

Rust ensures there is only one implementation of a specific trait for a specific type in the global scope/ in the entire program (Only One Implementation Per Type). This is primarily done to prevent multiple conflicting implementations across different layers of an application. This is called Coherence (or Trait Coherence). If multiple implementations exist, the code will not compile.

To prevent overwriting an existing implementation in a different layer of the application, Rust prohibits implementing a foreign trait for a foreign type. In other words, either the trait or the type must be defined in the current crate. This is called the Orphan Rule.

Suppose you want to implement a std trait for a std/ foreign type but Rust not allow it due to the orphan rule. In that case, we can create a brand-new type that wraps the foreign type and implements the trait for the wrapper type. This is called Newtype Pattern.

use anyhow::{Context, Result};
+use std::fmt;
+use std::time::{SystemTime, UNIX_EPOCH};
+
+struct Timestamp(SystemTime); // 💡 The newtype that wraps the std type
+
+impl Timestamp {
+    fn get_seconds(&self) -> Result<u64> {
+        self.0
+            .duration_since(UNIX_EPOCH)
+            .with_context(|| "Clock is set before 1970")
+            .map(|d| d.as_secs())
+    }
+}
+
+// 💡 Implement std trait, Display for newtype
+impl fmt::Display for Timestamp {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        let secs = self.get_seconds().map_err(|_| fmt::Error)?;
+        write!(f, "{secs}")
+    }
+}
+
+fn main() -> Result<()> {
+    let now = Timestamp(SystemTime::now());
+    println!("Current time: {}", now);
+
+    Ok(())
+}
+
\ No newline at end of file diff --git a/docs/docs/traits/og.jpg b/docs/docs/traits/og.jpg new file mode 100644 index 00000000..cb190537 Binary files /dev/null and b/docs/docs/traits/og.jpg differ diff --git a/docs/docs/unwrap-and-expect/index.html b/docs/docs/unwrap-and-expect/index.html new file mode 100644 index 00000000..89035bed --- /dev/null +++ b/docs/docs/unwrap-and-expect/index.html @@ -0,0 +1,162 @@ +Unwrap and Expect · Learning Rust

Unwrap and Expect

unwrap()

  • If an Option type has Some value or a Result type has a Ok value, the value inside them passes to the next step.
  • If the Option type has None value or the Result type has Err value, program panics; If Err, panics with the error message.

The functionality is bit similar to the following codes, which are using match instead unwrap().

Example with Option and match, before using unwrap()

fn main() {
+    let x;
+    match get_an_optional_value() {
+        Some(v) => x = v, // if Some("abc"), set x to "abc"
+        None => panic!(), // if None, panic without any message
+    }
+
+    println!("{}", x); // "abc" ; if you change line 14 `false` to `true`
+}
+
+fn get_an_optional_value() -> Option<&'static str> {
+
+    //if the optional value is not empty
+    if false {
+        return Some("abc");
+    }
+    
+    //else
+    None
+}
+
+
+// --------------- Compile-time error ---------------
+thread 'main' panicked at 'explicit panic', src/main.rs:5:17
+

Example with Result and match, before using unwrap()

fn main() {
+    let x;
+    match function_with_error() {
+        Ok(v) => x = v, // if Ok(255), set x to 255
+        Err(e) => panic!(e), // if Err("some message"), panic with error message "some message"
+    }
+
+    println!("{}", x); // 255 ; if you change line 13 `true` to `false`
+}
+
+fn function_with_error() -> Result<u64, String> {
+    //if error happens
+    if true {
+        return Err("some message".to_string());
+    }
+
+    // else, return valid output
+    Ok(255)
+}
+
+
+// ---------- Compile-time error ----------
+thread 'main' panicked at 'some message', src/main.rs:5:19
+

Same codes in above main functions can be written with unwrap() using two lines.

// 01. unwrap error message for None
+fn main() {
+    let x = get_an_optional_value().unwrap();
+
+    println!("{}", x);
+}
+
+// --------------- Compile-time error ---------------
+thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', libcore/option.rs:345:21
+
+
+// 02. unwrap error message for Err
+fn main() {
+    let x = function_with_error().unwrap();
+
+    println!("{}", x);
+}
+
+// --------------- Compile-time error ---------------
+thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "some message"', libcore/result.rs:945:5
+

⭐ But as you can see, when using unwrap() error messages are not showing the exact line numbers where the panic happens.

expect()

Similar to unwrap() but can set a custom message for the panics.

// 01. expect error message for None
+fn main() {
+    let n: Option<i8> = None;
+    
+    n.expect("empty value returned");
+}
+
+// --------------- Compile-time error ---------------
+thread 'main' panicked at 'empty value returned', libcore/option.rs:989:5
+
+
+// 02. expect error message for Err
+fn main() {
+    let e: Result<i8, &str> = Err("some message");
+
+    e.expect("expect error message");
+}
+
+// --------------- Compile-time error ---------------
+thread 'main' panicked at 'expect error message: "some message"', libcore/result.rs:945:5
+

unwrap_err() and expect_err() for Result types

The opposite case of unwrap() and expect(); Panics with Ok values, instead Err. Both print the value inside Ok on the error message.

💡 Usually use with tests.

// 01. unwrap_err error message for Ok
+fn main() {
+    let o: Result<i8, &str> = Ok(8);
+
+    o.unwrap_err();
+}
+
+// ---------- Compile-time error ----------
+thread 'main' panicked at 'called `Result::unwrap_err()` on an `Ok` value: 8', libcore/result.rs:945:5
+
+
+// 02. expect_err error message for Ok
+fn main() {
+    let o: Result<i8, &str> = Ok(8);
+
+    o.expect_err("Should not get Ok value");
+}
+
+// ---------- Compile-time error ----------
+thread 'main' panicked at 'Should not get Ok value: 8', libcore/result.rs:945:5
+

unwrap_or(), unwrap_or_default() and unwrap_or_else()

💡 These are bit similar to unwrap(), If an Option type has Some value or a Result type has a Ok value, the value inside them passes to the next step. But when having None or Err, the functionalities are bit different.

  • unwrap_or() : With None or Err, the value you passes to unwrap_or() is passing to the next step. But the data type of the value you passes should match with the data type of the relevant Some or Ok.
fn main() {
+    let v1 = 8;
+    let v2 = 16;
+
+    let s_v1 = Some(8);
+    let n = None;
+
+    assert_eq!(s_v1.unwrap_or(v2), v1); // Some(v1) unwrap_or v2 = v1
+    assert_eq!(n.unwrap_or(v2), v2);    // None unwrap_or v2 = v2
+
+    let o_v1: Result<i8, &str> = Ok(8);
+    let e: Result<i8, &str> = Err("error");
+
+    assert_eq!(o_v1.unwrap_or(v2), v1); // Ok(v1) unwrap_or v2 = v1
+    assert_eq!(e.unwrap_or(v2), v2);    // Err unwrap_or v2 = v2
+}
+
  • unwrap_or_default() : With None or Err, the default value of the data type of the relevant Some or Ok, is passing to the next step.
fn main() {
+    let v = 8;
+    let v_default = 0;
+
+    let s_v: Option<i8> = Some(8);
+    let n: Option<i8> = None;
+
+    assert_eq!(s_v.unwrap_or_default(), v);       // Some(v) unwrap_or_default = v
+    assert_eq!(n.unwrap_or_default(), v_default); // None unwrap_or_default = default value of v
+
+    let o_v: Result<i8, &str> = Ok(8);
+    let e: Result<i8, &str> = Err("error");
+
+    assert_eq!(o_v.unwrap_or_default(), v);       // Ok(v) unwrap_or_default = v
+    assert_eq!(e.unwrap_or_default(), v_default); // Err unwrap_or_default = default value of v
+}
+
  • unwrap_or_else() : Similar to unwrap_or(). The only difference is, instead of passing a value, you have to pass a closure which returns a value with the same data type of the relevant Some or Ok.
fn main() {
+    let v1 = 8;
+    let v2 = 16;
+
+    let s_v1 = Some(8);
+    let n = None;
+    let fn_v2_for_option = || 16;
+
+    assert_eq!(s_v1.unwrap_or_else(fn_v2_for_option), v1); // Some(v1) unwrap_or_else fn_v2 = v1
+    assert_eq!(n.unwrap_or_else(fn_v2_for_option), v2);    // None unwrap_or_else fn_v2 = v2
+
+    let o_v1: Result<i8, &str> = Ok(8);
+    let e: Result<i8, &str> = Err("error");
+    let fn_v2_for_result = |_| 16;
+
+    assert_eq!(o_v1.unwrap_or_else(fn_v2_for_result), v1); // Ok(v1) unwrap_or_else fn_v2 = v1
+    assert_eq!(e.unwrap_or_else(fn_v2_for_result), v2);    // Err unwrap_or_else fn_v2 = v2
+}
+
\ No newline at end of file diff --git a/docs/docs/unwrap-and-expect/og.jpg b/docs/docs/unwrap-and-expect/og.jpg new file mode 100644 index 00000000..16dada3c Binary files /dev/null and b/docs/docs/unwrap-and-expect/og.jpg differ diff --git a/docs/docs/use/index.html b/docs/docs/use/index.html new file mode 100644 index 00000000..4af7b28d --- /dev/null +++ b/docs/docs/use/index.html @@ -0,0 +1,95 @@ +Use · Learning Rust

Use

Let’s see the main usages of the use keyword.

01. Bind a full path to a new name

Mainly use keyword is used to bind a full path of an element to a new name. So the user doesn’t want to repeat the full path each time.

// -- Initial code without the `use` keyword --
+mod phrases { 
+  pub mod greetings { 
+    pub fn hello() { 
+      println!("Hello, world!");
+    }
+  }
+}
+
+fn main() { 
+  phrases::greetings::hello(); // Using full path
+}
+
+
+// -- Usage of the `use` keyword --
+// 01. Create an alias for module
+use phrases::greetings;
+fn main() { 
+  greetings::hello();
+}
+
+// 02. Create an alias for module elements
+use phrases::greetings::hello;
+fn main() { 
+  hello();
+}
+
+// 03. Customize names with the `as` keyword
+use phrases::greetings::hello as greet;
+fn main() { 
+  greet();
+}
+

02. Import elements to scope

Another common usage of use is importing elements to scope. Remember that, this is also a bit similar to creating an alias and using it instead of using the full path.

fn hello() -> String {
+  "Hello, world!".to_string()
+}
+
+#[cfg(test)]
+mod tests {
+  use super::hello; // Import the `hello()` function into the scope
+    
+  #[test]
+  fn test_hello() {
+    assert_eq!("Hello, world!", hello()); // If not using the above `use` statement, we can run same via `super::hello()`
+  }
+}
+

💡 By default, use declarations use absolute paths, starting from the crate root. But self and super declarations make that path relative to the current module.

Same way the use keyword is used to import the elements of other crates including the std, Rust’s Standard Library.

// -- 01. Importing elements --
+use std::fs::File;
+
+fn main() {
+    File::create("empty.txt").expect("Can not create the file!");
+}
+
+
+// -- 02. Importing module and elements --
+use std::fs::{self, File} // `use std::fs; use std::fs::File;`
+
+fn main() {
+    fs::create_dir("some_dir").expect("Can not create the directry!");
+    File::create("some_dir/empty.txt").expect("Can not create the file!");
+}
+
+
+// -- 03. Importing multiple elements --
+use std::fs::File;
+use std::io::{BufReader, BufRead}; // `use std::io::BufReader; use std::io::BufRead;`
+
+fn main() {
+    let file = File::open("src/hello.txt").expect("file not found");
+    let buf_reader = BufReader::new(file);
+
+    for line in buf_reader.lines() {
+        println!("{}", line.unwrap());
+    }
+}
+

We don’t need to use extern crate std; when using the std library. We will discuss more about this under the Standard Library section.

💡 use statements import only what we’ve specified into the scope, instead of importing all elements of a module or crate. So it improves the efficiency of the program.

03. Re-exporting

Another special case is pub use. When creating a module, you can export things from another module into your module. So after that, they can be accessed directly from your module. This is called re-exporting.

// ↳ main.rs
+mod phrases;
+
+fn main() {
+    phrases::hello(); // Not directly map
+}
+
+// ↳ phrases/mod.rs
+pub mod greetings;
+
+pub use self::greetings::hello; // Re-export `greetings::hello` to phrases
+
+// ↳ phrases/greetings.rs
+pub fn hello() {
+  println!("Hello, world!");
+}
+

This pattern is quite common in large libraries. It helps to hide the complexity of the internal module structure of the library from users. Because users don’t need to know/follow the whole directory map of the elements of the library while working with them.

\ No newline at end of file diff --git a/docs/docs/use/og.jpg b/docs/docs/use/og.jpg new file mode 100644 index 00000000..e2e54432 Binary files /dev/null and b/docs/docs/use/og.jpg differ diff --git a/docs/docs/variable-bindings-constants-and-statics/index.html b/docs/docs/variable-bindings-constants-and-statics/index.html new file mode 100644 index 00000000..c2c62169 --- /dev/null +++ b/docs/docs/variable-bindings-constants-and-statics/index.html @@ -0,0 +1,70 @@ +Variable bindings, Constants & Statics · Learning Rust

Variable bindings, Constants & Statics

  • Rust is a statically typed language; it checks data types at compile-time. But it doesn’t require you to actually type data types when declaring variable bindings. In that case, the compiler checks the usage and sets a better data type for it.
  • ⭐️ For constants and statics, we must annotate the data type.
  • Types come after a : (colon) sign.
  • The naming convention for the variable bindings is using the snake_case. But, for constants and statics, we should follow the SCREAMING_SNAKE_CASE.

💭 In the following examples, we will use data types like bool, i32, i64 and f64. Don’t worry about them for now; they’ll be discussed later.

Variable Bindings

  • The let keyword is used in binding expressions. We can bind a name to a value or a function. Also, because the left-hand side of a let expression is a “pattern”, you can bind multiple names to a set of values or function values.

  • In Rust, variables are immutable by default, so we call them Variable bindings. To make them mutable, the mut keyword is used.

Declaration & Assignment

let a; // Declaration; without data type
+a = 5; // Assignment
+
+let b: i8; // Declaration; with data type
+b = 5;
+
+let c = true;        // Declaration + assignment; without data type
+let d: bool = false; // Declaration + assignment; with data type
+
+let e = 4 + 2; // e = 6
+

Mutability

As mentioned, variable bindings are immutable by default. We need to add the mut keyword to make them mutable.

let mut a = 5; // a = 5
+a = a + 5; // a = 10
+

Multiple Declarations & Assignments

let (a, b); // Declaration
+(a, b) = (1, 2); // Assignment
+
// Declaration + assignment
+let (a, b) = (1, 2); // a = 1 and b = 2
+
// Declaration + assignment + mutability
+let (mut a, mut b) = (3, 4); // a = 3 and b = 4
+(a, b) = (a-b, a+b); // a = -1 and b = 7
+

Scope

let (a, b) = (1, 2); // a = 1 and b = 2
+
+let c = {
+    let a = 4; // affects inside wrapping {} only
+    let b = 6; // affects inside wrapping {} only
+
+    a + b
+}; // c = 10
+
+let d = { a + b }; // d = 3
+
+println!("{a} {b} {c} {d}"); // 1 2 10 3
+

Constants

The const keyword is used to define constants and after the assignment their values are not allowed to change. They live for the entire lifetime of a program but has no fixed address in the memory.

const N: i32 = 5;
+
+const DB_PORT: u16 = 5432;
+
+const SERVER_TIMEOUT: u32 = 60 * 5;
+

Statics

The static keyword is used to define a “global variable” type facility. There is only one instance for each value, and it’s at a fixed location in memory.

static N: i32 = 5;
+
+static DB_PORT: u16 = 5432;
+
+static SERVER_TIMEOUT: u32 = 60 * 5;
+

💭 While you need constants, always use const, instead of static. It’s pretty rare that you actually want a memory location associated with your constant, and using a const allows for optimizations like constant propagation, not only in your crate but also in downstream crates.

Variable Shadowing

Sometimes, while dealing with data, initially we get them in one unit but need to transform them into another unit for further processing. In this situation, instead of using different variable names, Rust allows us to redeclare the same variable with a different data type and/ or with a different mutability setting. We call this Shadowing.

let s: &str = "hello"; // &str
+let s: String = s.to_uppercase(); // String
+println!("{s}"); // HELLO
+
let (a, b) = (1, 2);
+let (a, b) = (b, a); // swap variables via shadowing
+println!("{a} {b}"); // 2 1
+
fn main() {
+    let a: f64 = -20.48; // float
+    let a: i64 = a.floor() as i64; // int
+
+    println!("{a}"); // -21
+
+    {
+        let a = a + 26; // affects inside wrapping {} scope only
+        println!("{a}"); // 5 💡 -21 + 26
+    }
+
+    println!("{a}"); // -21 💡 outer a
+}
+

👨‍🏫 Before going to the next…

  • Usually, constants and statics are placed at the top of the code file, outside the functions (after module imports/ use declarations).

    const PI: f64 = 3.14159265359;
    +
    +fn main() {
    +    println!("π value is {}", PI);
    +}
    +
\ No newline at end of file diff --git a/docs/docs/variable-bindings-constants-and-statics/og.jpg b/docs/docs/variable-bindings-constants-and-statics/og.jpg new file mode 100644 index 00000000..4551af7e Binary files /dev/null and b/docs/docs/variable-bindings-constants-and-statics/og.jpg differ diff --git a/docs/docs/vectors/index.html b/docs/docs/vectors/index.html new file mode 100644 index 00000000..53c015a2 --- /dev/null +++ b/docs/docs/vectors/index.html @@ -0,0 +1,85 @@ +Vectors · Learning Rust

Vectors

If you remember, the array is a fixed-size list of elements, of the same data type. Even with mut, its element count cannot be changed. A vector is kind of a re-sizable array but all elements must be in the same type.

💡 Vec<T>: capital “V” as it’s a struct.

It’s a generic type, written as Vec<T>. T can have any type, ex. A vector of i32s is Vec<i32>. Also, Vectors always allocate their data in a dynamically allocated heap.

Creation

Empty Vector

let mut a = Vec::new(); // 1. With new() keyword
+let mut b = vec![]; // 2. Using the vec! macro (💡 usually create with values same time)
+
+// ⭐️ If you need an immutable empty vector, you must have to specify the data type.
+let a: Vec<i32> = Vec::new();
+let b: Vec<String> = vec![];
+

With Type Annotations

let a: Vec<i32> = Vec::new();
+let b: Vec<i32> = vec![];
+
+let c = vec![1i32, 2, 3]; // Suffixing 1st value with data type
+

With Values

let a = vec![1, 2, 3];
+let b: Vec<i32> = vec![1, 2, 3];
+let c  = vec![1i32, 2, 3];  
+
let a = vec![0; 10]; // Ten zeroes
+let b = vec![""; 10]; // Ten "" str
+

With a Capacity

let mut a: Vec<i32> = Vec::with_capacity(10);
+println!("Length: {}, Capacity : {}", a.len(), a.capacity()); // Length: 0, Capacity : 10
+

💭 We’ll discuss this in the Length and Capacity.

Accessing and Changing Elements

By Index

We can access and change elements of a vector, via the index (like we access/ change elements of an array).

let mut a = vec![1, 2, 3];
+
+println!("{} {} {}", a[0], a[1], a[2]); // 1 2 3
+
+a[0] = 4; // [4, 2, 3]
+(a[1], a[2]) = (a[2], a[1]); // Shuffle
+
+println!("{:?}", a); // [4, 3, 2]
+
+a[5] = 2; // 💥 panics at runtime; index out of bounds: the len is 3 but the index is 5
+

By the get Method

Similar to accessing elements via the index, but safer, as it always returns an Option<T>/ optional value.

let mut a = vec![1, 2, 3];
+
+let x = a.get(0); // Some(1)
+let y = a.get(5); // None
+

💭 Option<T> can be either Some value or None (no value). It is also a generic type, like Vec<T>. We’ll discuss more details in the Generics: Option. For the moment, focus on vectors.

By the push and pop Methods

let mut a: Vec<i32> = Vec::new();
+
+a.push(1); // Add 1 to the end; a = [1]
+a.push(2); // Add 2 to the end; a = [1, 2]
+
+a.pop(); // Remove 2 from the end; a = [1]
+
+let x = a.pop(); // Remove 1 from the end and assign it to x as Option<T>; a = []
+// x = Some(1)
+
+let y = a.pop(); // Remove nothing as a is empty; a = [] ⭐️ No panics
+// y = None
+

Length and Capacity

In Rust, most types have a fixed size known at compile time and implement the trait Sized. Vec<T> is also a sized type; A struct that internally stores,

  1. A pointer: points to the heap-allocated memory storing the elements contiguously, like a slice [T]
  2. Length: NO of elements currently have
  3. Capacity: Amount of space allocated for any future elements

⭐️ If the length of a vector exceeds its capacity, its capacity will be increased automatically. But its elements will be reallocated(which can be slow). So, always use Vec::with_capacity whenever it’s possible.

let mut e: Vec<i32> = Vec::with_capacity(10); // Length: 0, Capacity : 10
+
+// These are all done without reallocating...
+for i in 0..10 {
+    e.push(i);
+}
+
+// ...but this may make the vector reallocate as exceeded current capacity
+e.push(11);
+

🔎 Dynamically sized types (DSTs)/ unsized types don’t have a fixed size at compile time, and the size is known only at run-time. Slices and trait objects are two examples of DSTs.

👨‍🏫 Before going to the next…

  • 💯 Vectors can be used with iterators in three ways,

    let mut a = vec![1, 2, 3, 4, 5];
    +
    +for i in a {
    +    println!("Take ownership of the vector and its element {}", i);
    +}
    +
    +for i in &a {
    +    println!("A reference to {}", i);
    +}
    +
    +for i in &mut a {
    +    println!("A mutable reference to {}", i);
    +}
    +
  • 💯 The String/ &str data types are UTF-8 encoded vectors. But you can not index into a String because of encoding.

    ⭐️ We can iterate over the characters of a string via the chars() method. But for more accurate results, you should use a crate like unicode_segmentation that follows more accurate Unicode text segmentation standards.

    let a = String::from("Hello!");
    +print!("{}", a.chars().count()); // 6
    +// 💡 H   e   l   l   o   !
    +
    let a = "a̐éö̲\r\n";
    +for (i, v) in a.chars().enumerate() {
    +    println!("{i}: {v}");
    +}
    +
    +// 0: a
    +// 1: ̐
    +// 2: é
    +// 3: ö
    +// 4: ̲
    +// 5:  
    +// 6:  
    +
\ No newline at end of file diff --git a/docs/docs/vectors/og.jpg b/docs/docs/vectors/og.jpg new file mode 100644 index 00000000..c36be3be Binary files /dev/null and b/docs/docs/vectors/og.jpg differ diff --git a/docs/docs/why-rust/index.html b/docs/docs/why-rust/index.html new file mode 100644 index 00000000..9539f3df --- /dev/null +++ b/docs/docs/why-rust/index.html @@ -0,0 +1,5 @@ +Why Rust? · Learning Rust

Why Rust?

History of Rust

Rust was initially designed and developed by former Mozilla employee Graydon Hoare as a personal project. Mozilla began sponsoring the project in 2009 and announced it in 2010. But the first stable release, Rust 1.0, was released on May 15, 2015.

Since Rust 1.0, major updates have been released as Editions approximately every three years: Rust 2015 (with the release of Rust 1.0) , Rust 2018, Rust 2021, and Rust 2024, all maintaining backward compatibility.

Initial Goals

Rust is a systems programming language focused on three goals: safety, speed, and concurrency.
~ Rust Documentation

Rust is a modern, multi-platform, multi-paradigm, statically compiled, memory & thread safety–focused, systems programming language.

  • It uses LLVM on the backend and supports many different operating systems, architectures, targets, and cross-compiling.

  • It supports imperative procedural, concurrent actor, object-oriented, and pure functional styles. Rust also supports generic programming and metaprogramming, in both static and dynamic styles.

  • It doesn’t use a built-in runtime or an automated garbage collection system (GC).

    💡 However, async Rust requires an async runtime, which is provided by community-maintained crates like tokio, soml etc. The async runtime will be bundled into the final executable.

  • One of Rust’s most unique and compelling features is Ownership, which is used to achieve memory safety. Rust creates memory pointers optimistically, checks memory pointers’ limited accesses at compile-time with the usage of References and Borrowing. And it does automatic compile-time memory management by checking the Lifetimes.

Influences

Its design elements came from a wide range of sources.

  • Abstract Machine Model: C
  • Data types: C, SML, OCaml, Lisp, Limbo
  • Optional Bindings: Swift
  • Hygienic Macros: Scheme
  • Functional Programming: Haskell, OCaml, F#
  • Attributes: ECMA-335
  • Memory Model and Memory Management: C++, ML Kit, Cyclone
  • Type Classes: Haskell
  • Crate: Assembly in the ECMA-335 CLI model
  • Channels and Concurrency: Newsqueak, Alef, Limbo
  • Message passing and Thread failure: Erlang

and etc.

Rust compiler observes the code at compile-time and helps to prevent many types of errors that are possible to write in C, C++ like programming languages.

👨‍🏫 Before going to the next…

\ No newline at end of file diff --git a/docs/docs/why-rust/og.jpg b/docs/docs/why-rust/og.jpg new file mode 100644 index 00000000..8e6df777 Binary files /dev/null and b/docs/docs/why-rust/og.jpg differ diff --git a/docs/docs/workspaces/index.html b/docs/docs/workspaces/index.html new file mode 100644 index 00000000..ffbba10d --- /dev/null +++ b/docs/docs/workspaces/index.html @@ -0,0 +1,55 @@ +Workspaces · Learning Rust

Workspaces

When the code base is getting larger, you might need to work with multiple crates on the same project. Rust supports this via Workspaces. You can analyze (cargo check), build, run tests or generate docs for all crates at once by running cargo commands from the project root.

⭐️ When working on multiple crates same time, there is a higher possibility of having shared dependencies on crates. To prevent downloading and compiling the same dependency multiple times, Rust uses a shared build directory under the project root, while running cargo build from the project root.

Let’s create a library crate with a simple hello world function and a binary crate which uses the library crate.

Assume we run,

mkdir greetings
+touch greetings/Cargo.toml
+cargo new greetings/lib --lib
+cargo new greetings/examples/hello
+

That generates,

greetings
+ ├── Cargo.toml
+ ├── examples
+ │  └── hello
+ │     ├── Cargo.toml
+ │     └── src
+ │        └── main.rs
+ └── lib
+    ├── Cargo.toml
+    └── src
+       └── lib.rs
+

We have to modify the following files,

// 01. greetings/Cargo.toml to mark as a workspace and to add members
+[workspace]
+members = [
+    "lib",
+    "examples/hello"
+]
+
+// 02.1 greetings/lib/Cargo.toml to change the package name to greetings
+[package]
+name = "greetings"
+version = "0.1.0"
+authors = ["Dumindu Madunuwan"]
+
+[dependencies]
+
+// 02.2 greetings/lib/src/lib.rs to add a simple hello world function
+pub fn hello() {
+    println!("Hello, world!");
+}
+
+// 03.1 greetings/examples/hello/Cargo.toml to add the `greetings` lib as a dependency
+[package]
+name = "hello"
+version = "0.1.0"
+authors = ["Dumindu Madunuwan"]
+
+[dependencies]
+greetings = { path = "../../lib" }
+
+// 03.2 greetings/examples/hello/src/main.rs to import the `greetings` lib and call its hello world function
+extern crate greetings;
+
+fn main() {
+    greetings::hello();
+}
+

💡 On Linux and Mac, you can run cargo commands on each crate without changing the working directory all the times via Subshells (A command list embedded between parentheses). For example, if you are in the greetings directory, even you run (cd examples/hello && cargo run) your working directory will be kept as same in greetings folder.

🔎 rust-lang/rust library folder is a good example for a workspace.

\ No newline at end of file diff --git a/docs/docs/workspaces/og.jpg b/docs/docs/workspaces/og.jpg new file mode 100644 index 00000000..c3423195 Binary files /dev/null and b/docs/docs/workspaces/og.jpg differ diff --git a/docs/favicon/android-chrome-192x192.png b/docs/favicon/android-chrome-192x192.png new file mode 100644 index 00000000..918f1d3b Binary files /dev/null and b/docs/favicon/android-chrome-192x192.png differ diff --git a/docs/favicon/android-chrome-512x512.png b/docs/favicon/android-chrome-512x512.png new file mode 100644 index 00000000..e46db077 Binary files /dev/null and b/docs/favicon/android-chrome-512x512.png differ diff --git a/docs/favicon/apple-touch-icon.png b/docs/favicon/apple-touch-icon.png new file mode 100644 index 00000000..84816ca1 Binary files /dev/null and b/docs/favicon/apple-touch-icon.png differ diff --git a/docs/favicon/favicon-16x16.png b/docs/favicon/favicon-16x16.png new file mode 100644 index 00000000..59a58e0f Binary files /dev/null and b/docs/favicon/favicon-16x16.png differ diff --git a/docs/favicon/favicon-32x32.png b/docs/favicon/favicon-32x32.png new file mode 100644 index 00000000..a680bb81 Binary files /dev/null and b/docs/favicon/favicon-32x32.png differ diff --git a/docs/favicon/favicon.ico b/docs/favicon/favicon.ico new file mode 100644 index 00000000..4f9e2b4e Binary files /dev/null and b/docs/favicon/favicon.ico differ diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 00000000..45d2a8f7 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,3 @@ +Learning Rust · Rust Programming Language Tutorials for Everyone!

Rust Illuminated.Grasp Quickly.

Rust Programming Language Tutorials for Everyone!

Get Started +GitHub
\ No newline at end of file diff --git a/docs/index.xml b/docs/index.xml new file mode 100644 index 00000000..e4e8581e --- /dev/null +++ b/docs/index.xml @@ -0,0 +1,453 @@ +Learning Rusthttps://learning-rust.github.io/Recent content on Learning RustHugoen-USBorrowinghttps://learning-rust.github.io/docs/borrowing/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/borrowing/<p>In real life applications, most of the times we have to pass variable bindings to other functions or assign them to other variable bindings. In this case, we are <strong>referencing</strong> the original binding; <strong>borrow</strong> the data of it.</p> +<h2 id="what-is-borrowing">What is Borrowing?</h2> +<blockquote> +<p><a href="https://github.com/nikomatsakis/rust-tutorials-keynote/blob/master/Ownership%20and%20Borrowing.pdf" target="_blank" >Borrow (verb)</a><br> +To receive something with the promise of returning it.</p> +</blockquote> +<h2 id="shared--mutable-borrowings">Shared &amp; Mutable borrowings</h2> +<p>⭐️ There are two types of Borrowing,</p> +<ol> +<li> +<p><strong>Shared Borrowing</strong> <code>(&amp;T)</code></p> +<ul> +<li>A piece of data can be <strong>borrowed by a single or multiple users</strong>, but <strong>data should not be altered</strong>.</li> +</ul> +</li> +<li> +<p><strong>Mutable Borrowing</strong> <code>(&amp;mut T)</code></p>Cargo, Crates and Basic Project Structurehttps://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/<h2 id="cargo">Cargo</h2> +<p>Cargo is Rust’s built-in package manager and build system. It also supports the following actions,</p> +<table> + <thead> + <tr> + <th>Command</th> + <th>Action</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>cargo new</code></td> + <td>Create a new project</td> + </tr> + <tr> + <td><code>cargo init</code></td> + <td>Create a new project in an existing directory</td> + </tr> + <tr> + <td><code>cargo check</code></td> + <td>Verify the project compiles without errors</td> + </tr> + <tr> + <td><code>cargo build</code></td> + <td>Build the executable</td> + </tr> + <tr> + <td><code>cargo run</code></td> + <td>Build the executable and run</td> + </tr> + <tr> + <td><code>cargo clean</code></td> + <td>Remove the build system directories/ <code>target</code> directory</td> + </tr> + </tbody> +</table> +<blockquote> +<p>💡 The <code>cargo check</code> command verifies that the project compiles without errors, without producing an executable. +Thus, it is often faster than <code>cargo build</code>.</p>Combinatorshttps://learning-rust.github.io/docs/combinators/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/combinators/<h2 id="what-is-a-combinator">What is a combinator?</h2> +<ul> +<li> +<p>One meaning of “combinator” is a more informal sense referring to the <strong>combinator pattern</strong>, a style of organizing libraries centered around the idea of combining things. Usually there is <strong>some type T</strong>, some <strong>functions for constructing “primitive” values of type T</strong>, and some “<strong>combinators</strong>” which can <strong>combine values of type T</strong> in various ways to <strong>build up more complex values of type T</strong>. The other definition is <strong>&ldquo;function with no free variables&rdquo;</strong>. +__ <a href="https://wiki.haskell.org/Combinator" target="_blank" >wiki.haskell.org</a></p>Comments and Documenting the codehttps://learning-rust.github.io/docs/comments-and-documenting-the-code/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/comments-and-documenting-the-code/<h2 id="comments">Comments</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="c1">// Line comments +</span></span></span><span class="line"><span class="cl"><span class="cm">/* Block comments */</span><span class="w"> +</span></span></span></code></pre></div><p>Nested block comments are supported.</p> +<p>💡 <strong>By convention, try to avoid using block comments. Use line comments instead.</strong></p> +<h2 id="doc-comments">Doc Comments</h2> +<p><a href="https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/#cargo" >As we discussed</a>, we can generate the project documentation via <a href="https://doc.rust-lang.org/stable/rustdoc/" target="_blank" >rustdoc</a> by running the <strong><code>cargo doc</code></strong> command. It uses the doc comments to generate the documentation.</p> +<p>💡 Usually we are adding doc comments on library crates. Also, we can use <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet" target="_blank" >Markdown notations</a> inside the doc comments.</p>Control Flowshttps://learning-rust.github.io/docs/control-flows/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/control-flows/<h2 id="if---else-if---else">if - else if - else</h2> +<h3 id="if"><code>if</code></h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">13</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">if</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">18</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, child!&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// The code prints this +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="if-else"><code>if</code> <code>else</code></h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">if</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Even&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Odd&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// The code prints this +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-let-statements">With <code>let</code> Statements</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">age</span>: <span class="kt">u8</span> <span class="o">=</span><span class="w"> </span><span class="mi">13</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">is_below_eighteen</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">18</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// true +</span></span></span></code></pre></div><h3 id="if-else-if-else"><code>if</code> <code>else if</code> <code>else</code></h3> +<p>i. A simple example,</p>Crateshttps://learning-rust.github.io/docs/crates/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/crates/<p>💭 Crates are a bit similar to the packages in some other languages. Crates compile individually. If the crate has child file modules, those files will get merged with the crate file and compile as a single unit.</p> +<p>💭 A crate can produce an executable/ a binary or a library. <code>src/main.rs</code> is the crate root/ entry point for a binary crate and <code>src/lib.rs</code> is the entry point for a library crate.</p>Custom Error Typeshttps://learning-rust.github.io/docs/custom-error-types/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/custom-error-types/<p>Rust allow us to create our own <code>Err</code> types. We call them “<em>Custom Error Types</em>”.</p> +<h2 id="error-trait">Error trait</h2> +<p>As you know <strong>traits define the functionality a type must provide</strong>. But we don’t always need to define new traits for common functionalities, because Rust <strong>standard library provides reusable traits</strong> which can be implemented on our own types. While creating custom error types the <a href="https://doc.rust-lang.org/std/error/trait.Error.html" target="_blank" ><code>std::error::Error</code> trait</a> helps us to convert any type to an <code>Err</code> type.</p>Enumshttps://learning-rust.github.io/docs/enums/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/enums/<ul> +<li>An enum is a single type that contains variants, which represent the possible values of the enum at any given time.</li> +<li>By convention, the enum name and its variants&rsquo; names should follow <a href="https://en.wikipedia.org/wiki/Camel_case" target="_blank" ><code>PascalCase</code></a>.</li> +<li>Can access the variants using the <code>::</code> notation and the variant name. ex. Day::Sunday</li> +</ul> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">enum</span> <span class="nc">Day</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Sunday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Monday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Tuesday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Wednesday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Thursday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Friday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Saturday</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 💡 Day is the enum. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday are its variants. +</span></span></span></code></pre></div><ul> +<li>An enum variant can have either, +<ul> +<li>No data (a unit variant)</li> +<li>Unnamed ordered data (a tuple variant)</li> +<li>Named data/ fields (a struct variant)</li> +</ul> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">enum</span> <span class="nc">FlashMessage</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Success</span><span class="p">,</span><span class="w"> </span><span class="c1">// 💡 A unit variant (no data) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Error</span><span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="nb">String</span><span class="p">),</span><span class="w"> </span><span class="c1">// 💡 A tuple variant (one or more , separated data) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">Warning</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">field</span>: <span class="nb">String</span><span class="p">,</span><span class="w"> </span><span class="n">message</span>: <span class="nb">String</span> <span class="p">},</span><span class="w"> </span><span class="c1">// 💡 A struct variant (one or more , separated name: value data) +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 💡 FlashMessage is the emnum, Success, Error, Warning are its variants. +</span></span></span></code></pre></div></li> +</ul> +<blockquote> +<p>💡 In Rust, the term &ldquo;instantiation&rdquo; is used to describe the act of creating a concrete instance of a type (struct or enum).</p>Error and None Propagationhttps://learning-rust.github.io/docs/error-and-none-propagation/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/error-and-none-propagation/<p>We should use panics like <code>panic!()</code>, <code>unwrap()</code>, <code>expect()</code> only if we can not handle the situation in a better way. Also if a function contains expressions which can produce either <code>None</code> or <code>Err</code>,</p> +<ul> +<li>we can handle them inside the same function. Or,</li> +<li>we can return <code>None</code> and <code>Err</code> types immediately to the caller. So the caller can decide how to handle them.</li> +</ul> +<p>💡 <code>None</code> types no need to handle by the caller of the function always. But Rusts’ convention to handle <strong><code>Err</code></strong> types is, <strong>return them immediately to the caller to give more control to the caller to decide how to handle them.</strong></p>Functionshttps://learning-rust.github.io/docs/functions/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/functions/<h2 id="named-functions">Named Functions</h2> +<ul> +<li>Named functions are declared with the keyword <strong><code>fn</code></strong></li> +<li>When using <strong>arguments</strong>, we <strong>must declare the data types</strong>.</li> +<li>By default, functions <strong>return an empty <a href="https://learning-rust.github.io/docs/primitive-data-types/#tuple" >tuple</a>/ <code>()</code></strong>. If you want to return a value, the <strong>return type must be specified</strong> after <strong><code>-&gt;</code></strong></li> +</ul> +<h3 id="hello-world">Hello world</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="passing-arguments">Passing Arguments</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">print_sum</span><span class="p">(</span><span class="n">a</span>: <span class="kt">i8</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">i8</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;sum is: </span><span class="si">{}</span><span class="s">&#34;</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="returning-values">Returning Values</h3> +<ul> +<li> +<p>Without the <code>return</code> keyword. Only the last expression returns.</p>Genericshttps://learning-rust.github.io/docs/generics/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/generics/<ul> +<li>The core concept of generics is abstraction over types. They let us write one piece of code to operate with any data type without repeating ourselves to write separate versions for each type. At the compile time, Rust ensures the type safety and generates an optimized code for each concrete type used in the program.</li> +<li>Use an uppercase letter (<code>T</code>, <code>U</code>, &hellip;) or a <a href="https://en.wikipedia.org/wiki/Camel_case" target="_blank" ><code>PascalCase</code></a> identifier for the data type. +<ul> +<li>Instead of <code>x: u8</code> we use <code>x: T</code>.</li> +<li>Inform the compiler that <code>T</code> is a generic type by adding <code>&lt;T&gt;</code> at first.</li> +</ul> +</li> +</ul> +<h2 id="with-one-generic-type">With One Generic Type</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Point</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">y</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">to_tuple</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// a: Point&lt;i32&gt; +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">to_tuple</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// (i32, i32) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{b:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// (0, 1) +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="nc">false</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">true</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// a: Point&lt;bool&gt; +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">to_tuple</span><span class="p">(</span><span class="n">c</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">.</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// (bool, bool) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{d:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// (false, true) +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h2 id="with-multiple-generic-types">With Multiple Generic Types</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Point</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">y</span>: <span class="nc">U</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">to_shuffled_tuple</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">U</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="n">U</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">1</span><span class="k">u8</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="nc">true</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="c1">// a: Point&lt;u8, bool&gt; +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">to_shuffled_tuple</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">y</span><span class="p">);</span><span class="w"> </span><span class="c1">// (bool, u8) +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{b:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> </span><span class="c1">// (true, 1) +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>On some occasions, the compiler cannot inter the type, and we have to specify the type when using the generic type. By the way, it&rsquo;s good practice to specify the type on variables when using a generic implementation.</p>Hello Worldhttps://learning-rust.github.io/docs/hello-world/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/hello-world/<h2 id="hello-world">Hello, World!</h2> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p><code>fn</code> means function. The <code>main</code> function is the beginning of every Rust program. +<code>println!()</code> prints text to the console and its <code>!</code> indicates that it’s a <a href="https://doc.rust-lang.org/book/ch19-06-macros.html" target="_blank" >macro</a> rather than a function.</p> +<blockquote> +<p>💡 Rust files should have <code>.rs</code> file extension and if you’re using more than one word for the file name, follow the <a href="https://en.wikipedia.org/wiki/Snake_case" target="_blank" >snake_case</a> convention.</p> +</blockquote> +<ul> +<li>Save the above code in <code>file.rs</code> , but it can be any name with <code>.rs</code> extension.</li> +<li>Compile it with <code>rustc file.rs</code></li> +<li>Execute it with <code>./file</code> on Linux and Mac or <code>file.exe</code> on Windows</li> +</ul> +<h2 id="rust-playground">Rust Playground</h2> +<p><a href="https://play.rust-lang.org/" target="_blank" >Rust Playground</a> is a web interface for running Rust code.</p>Implshttps://learning-rust.github.io/docs/impls/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/impls/<ul> +<li>Earlier, we discussed that structs and enums group related data, while impl blocks and traits add associated and shared behavior to the data.</li> +<li>Usage of <code>Self</code> vs <code>self</code> keywords: +<ul> +<li><code>Self</code>: Refers to the type itself (the blueprint).</li> +<li><code>self</code>: Refers to the instance of the type (the actual data). +<ul> +<li>💯 This can be any form of <code>self</code>, <code>&amp;self</code>, <code>&amp;mut self</code>, <code>self: Box&lt;Self&gt;</code>, <code>self: Pin&lt;&amp;mut Self&gt;</code>, etc.</li> +</ul> +</li> +</ul> +</li> +<li>There are multiple ways to implement a behavior for a type. We discuss only about the <code>impl</code> blocks with this article. The patterns involving traits are discussed under <a href="https://learning-rust.github.io/docs/traits" >Traits</a>.</li> +</ul> +<h2 id="inherent-impls">Inherent impls</h2> +<p>Implement associated functions, methods, and constants directly for a type.</p>Installationhttps://learning-rust.github.io/docs/installation/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/installation/<h2 id="rustup">Rustup</h2> +<p>There are many ways to install Rust on your system. For the moment the official way to install Rust is using <a href="https://rustup.rs/" target="_blank" >Rustup</a>.</p> +<p><a href="https://rust-lang.github.io/rustup/index.html" target="_blank" >📖</a> Rustup installs The Rust Programming Language from the official release channels, enabling you to easily switch between <strong>stable, beta, and nightly</strong> compilers and keep them updated. It also makes cross-compiling simpler with binary builds of the standard library for common platforms.</p> +<p><a href="https://rust-lang.github.io/rustup/installation/index.html" target="_blank" >📖</a> Rustup installs <code>rustc</code>, <code>cargo</code>, <code>rustup</code> and other standard tools to <strong>Cargo&rsquo;s <code>bin</code> directory</strong>. On Unix it is located at <code>$HOME/.cargo/bin</code> and on Windows at <code>%USERPROFILE%\.cargo\bin</code>. This is the same directory that <code>cargo install</code> will install Rust programs and Cargo plugins.</p>Lifetimeshttps://learning-rust.github.io/docs/lifetimes/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/lifetimes/<p>When we are dealing with references, we have to make sure that the referencing data stay alive until we stop using the references.</p> +<p>Think,</p> +<ul> +<li>We have a <strong>variable binding</strong>, <code>a</code>.</li> +<li>We are <strong>referencing</strong> the value of <code>a</code>, <strong>from another variable binding</strong> <code>x</code>. +We have to make sure that <strong><code>a</code> lives until we stop using <code>x</code></strong>.</li> +</ul> +<blockquote> +<p>🔎 <strong>Memory management</strong> is a form of resource management applied to computer memory. Up until the mid-1990s, the majority of programming languages used <strong>Manual Memory Management</strong> which <strong>requires the programmer to give manual instructions</strong> to identify and deallocate unused objects/ garbage. Around 1959 John McCarthy invented <strong>Garbage collection</strong>(GC), a form of <strong>Automatic Memory Management</strong>(AMM). It determines what memory is no longer used and frees it automatically instead of relying on the programmer. However <strong>Objective-C and Swift</strong> provide similar functionality through <strong>Automatic Reference Counting</strong>(ARC).</p>Moduleshttps://learning-rust.github.io/docs/modules/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/modules/<h2 id="01-in-the-same-file">01. In the same file</h2> +<p>Related code and data are grouped into a module and stored in the same file.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">mod</span> <span class="nn">greetings</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="c1">// ⭐️ By default, everything inside a module is private +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// ⭐️ So function has to be public to access from outside +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>Modules can also be nested.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">mod</span> <span class="nn">phrases</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">greetings</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>Private functions can be called from the same module or from a child module.</p>Operatorshttps://learning-rust.github.io/docs/operators/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/operators/<h2 id="arithmetic-operators">Arithmetic Operators</h2> +<p><code>+ - * / %</code></p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// 6 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// 4 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 10 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// ⭐️ 2 not 2.5 +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="c1">// 1 +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">5.0</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2.0</span><span class="p">;</span><span class="w"> </span><span class="c1">// 2.5 +</span></span></span></code></pre></div><h2 id="comparison-operators">Comparison Operators</h2> +<p><code>== != &lt; &gt; &lt;= &gt;=</code></p>Option and Resulthttps://learning-rust.github.io/docs/option-and-result/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/option-and-result/<h2 id="why-option-and-result">Why Option and Result?</h2> +<p>Many languages use <strong><code>null</code>\ <code>nil</code>\ <code>undefined</code> types</strong> to represent empty outputs, and <strong><code>Exceptions</code></strong> to handle errors. Rust skips using both, especially to prevent issues like <strong>null pointer exceptions, sensitive data leakages through exceptions</strong>, etc. Instead, Rust provides two special <strong>generic enums</strong>;<code>Option</code> and <code>Result</code> to deal with above cases.</p> +<blockquote> +<p>💭 In the previous sections, we have discussed about the basics of <a href="https://learning-rust.github.io/docs/enums" >enums</a>, <a href="https://learning-rust.github.io/docs/generics" >generics</a> and <a href="https://learning-rust.github.io/docs/generics/#generalizing-enums" ><code>Result</code> &amp; <code>Option</code> types</a>.</p>Overviewhttps://learning-rust.github.io/docs/overview/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/overview/<h2 id="about-me">About me</h2> +<blockquote> +<p>🧑‍💻 I am an expat working in Singapore as a Go Backend and DevOps Engineer. Feel free to reach out if you find any mistakes or anything that needs to be changed, including spelling or grammar errors. Alternatively, you can create a pull request, open an issue, or <a href="https://gist.github.com/dumindu/00a0be2d175ed5ff3bc3c17bbf1ca5b6" target="_blank" >share your awesome ideas in this gist</a>. Good luck with learning Rust!</p> +</blockquote> +<p><a href="https://github.com/learning-rust/learning-rust.github.io" target="_blank" ><img src="https://img.shields.io/github/stars/learning-rust/learning-rust.github.io?style=for-the-badge&amp;logo=rust&amp;label=learning-rust.github.io&amp;logoColor=333333&amp;labelColor=f9f9f9&amp;color=F46623" alt="learning-rust.github.io"></a> +<a href="https://learning-cloud-native-go.github.io" target="_blank" ><img src="https://img.shields.io/github/stars/learning-cloud-native-go/learning-cloud-native-go.github.io?style=for-the-badge&amp;logo=go&amp;logoColor=333333&amp;label=learning-cloud-native-go.github.io&amp;labelColor=f9f9f9&amp;color=00ADD8" alt="learning-cloud-native-go.github.io"></a></p> +<p><a href="https://github.com/dumindu" target="_blank" ><img src="https://img.shields.io/badge/dumindu-866ee7?style=for-the-badge&amp;logo=GitHub&amp;logoColor=333333&amp;labelColor=f9f9f9" alt="github.com"></a> +<a href="https://www.buymeacoffee.com/dumindu" target="_blank" ><img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-dumindu-FFDD00?style=for-the-badge&amp;logo=buymeacoffee&amp;logoColor=333333&amp;labelColor=f9f9f9" alt="buymeacoffee"></a></p> +<h2 id="overview">Overview</h2> +<p>This publication has its origins in the posts I authored on Medium at <a href="https://medium.com/learning-rust" target="_blank" >https://medium.com/learning-rust</a>. However, please note that I have ceased updating the Medium posts. All current and future updates, new content, code, and grammar fixes will be exclusively maintained and released here, <a href="https://learning-rust.github.io" target="_blank" >https://learning-rust.github.io</a>.</p>Ownershiphttps://learning-rust.github.io/docs/ownership/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/ownership/<p>We discussed in the <a href="https://learning-rust.github.io/docs/traits/#derive-traits" >Derive Traits</a>, the usage of <a href="https://doc.rust-lang.org/std/marker/trait.Copy.html" target="_blank" ><code>Copy</code> marker trait</a> and <a href="https://doc.rust-lang.org/std/clone/index.html" target="_blank" ><code>.clone()</code></a> with the below code.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="cp">#[derive(Debug, Clone, Copy)]</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">x</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">y</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{a:?}</span><span class="s">, </span><span class="si">{b:?}</span><span class="s">&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><p>If we try to remove <code>Copy</code> derive on <code>Point</code> and run, we will get the following error while compiling.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="n">error</span><span class="p">[</span><span class="n">E0382</span><span class="p">]</span>: <span class="nc">borrow</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">value</span>: <span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">main</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">11</span>:<span class="mi">16</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Point</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span>: <span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="k">move</span><span class="w"> </span><span class="n">occurs</span><span class="w"> </span><span class="n">because</span><span class="w"> </span><span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="k">type</span> <span class="err">`</span><span class="n">Point</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">does</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">implement</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="err">`</span><span class="nb">Copy</span><span class="err">`</span><span class="w"> </span><span class="k">trait</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="mi">9</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">here</span><span class="w"> +</span></span></span></code></pre></div><p>This is because of Ownership, which is used to achieve Rust&rsquo;s memory safety.</p>Panickinghttps://learning-rust.github.io/docs/panicking/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/panicking/<h2 id="panic">panic!()</h2> +<ul> +<li>In some cases, when an error occurs we can not do anything to handle it, <strong>if the error is something which should not have happened</strong>. In other words, if it’s an <strong>unrecoverable error</strong>.</li> +<li>Also <strong>when we are not using a feature-rich debugger or proper logs</strong>, sometimes we need to <strong>debug the code by quitting the program from a specific line of code</strong> by printing out a specific message or a value of a variable binding to understand the current flow of the program. +For above cases, we can use <code>panic!</code> macro.</li> +</ul> +<p>⭐ <code>panic!()</code> runs <strong>thread based</strong>. One thread can be panicked, while other threads are running.</p>Primitive Data Typeshttps://learning-rust.github.io/docs/primitive-data-types/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/primitive-data-types/<h2 id="bool">bool</h2> +<p>true or false</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="kt">bool</span> <span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ⭐️ no TRUE, FALSE, 1, 0 +</span></span></span></code></pre></div><p>bool is a single byte(8 bits) in size.</p> +<h2 id="char">char</h2> +<p>A single Unicode scalar value</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="sc">&#39;x&#39;</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="kt">char</span> <span class="o">=</span><span class="w"> </span><span class="sc">&#39;😎&#39;</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ⭐️ no &#34;x&#34;, only single quotes +</span></span></span></code></pre></div><p>Because of Unicode support, char is not a single byte, but four(32 bits).</p> +<h2 id="i8-i16-i32-i64-i128">i8, i16, i32, i64, i128</h2> +<p>8, 16, 32, 64 and 128 bit fixed sized signed(+/-) integer types</p>Smart Compilerhttps://learning-rust.github.io/docs/smart-compiler/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/smart-compiler/<h2 id="why-compiler">Why Compiler?</h2> +<p>The Rust compiler does the most significant job to prevent errors in Rust programs. It <strong>analyzes the code at compile-time</strong> and issues warnings, if the code does not follow memory management rules or lifetime annotations correctly.</p> +<p>For example,</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="cp">#[allow(unused_variables)]</span><span class="w"> </span><span class="c1">//💡 A lint attribute used to suppress the warning; unused variable: `b` +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">];</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{:?}</span><span class="s">&#34;</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ------ Compile-time error ------ +</span></span></span><span class="line"><span class="cl"><span class="n">error</span><span class="p">[</span><span class="n">E0382</span><span class="p">]</span>: <span class="nc">use</span><span class="w"> </span><span class="n">of</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">value</span>: <span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">main</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">6</span>:<span class="mi">22</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="mi">3</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="n">moved</span><span class="w"> </span><span class="n">here</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="mi">4</span><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="mi">5</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{:?}</span><span class="s">&#34;</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">^</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="n">used</span><span class="w"> </span><span class="n">here</span><span class="w"> </span><span class="n">after</span><span class="w"> </span><span class="k">move</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">|</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">note</span>: <span class="nc">move</span><span class="w"> </span><span class="n">occurs</span><span class="w"> </span><span class="n">because</span><span class="w"> </span><span class="err">`</span><span class="n">a</span><span class="err">`</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="k">type</span> <span class="err">`</span><span class="n">std</span>::<span class="n">vec</span>::<span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="err">`</span><span class="p">,</span><span class="w"> </span><span class="n">which</span><span class="w"> </span><span class="n">does</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">implement</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="err">`</span><span class="nb">Copy</span><span class="err">`</span><span class="w"> </span><span class="k">trait</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="n">error</span>: <span class="nc">aborting</span><span class="w"> </span><span class="n">due</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">previous</span><span class="w"> </span><span class="n">error</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="n">For</span><span class="w"> </span><span class="n">more</span><span class="w"> </span><span class="n">information</span><span class="w"> </span><span class="n">about</span><span class="w"> </span><span class="n">this</span><span class="w"> </span><span class="n">error</span><span class="p">,</span><span class="w"> </span><span class="kr">try</span><span class="w"> </span><span class="err">`</span><span class="n">rustc</span><span class="w"> </span><span class="o">--</span><span class="n">explain</span><span class="w"> </span><span class="n">E0382</span><span class="err">`</span><span class="p">.</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// ⭐ instead using #[allow(unused_variables)], consider using &#34;let _b = a;&#34; in line 4. +</span></span></span><span class="line"><span class="cl"><span class="c1">// Also you can use &#34;let _ =&#34; to completely ignore return values +</span></span></span></code></pre></div><blockquote> +<p>💭 In the previous sections, we have discussed memory management concepts like <a href="https://learning-rust.github.io/docs/ownership" >ownership</a>, <a href="https://learning-rust.github.io/docs/borrowing" >borrowing</a>, <a href="https://learning-rust.github.io/docs/lifetimes" >lifetimes</a> and etc.</p>STD, Primitives and Preludeshttps://learning-rust.github.io/docs/std-primitives-and-preludes/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/std-primitives-and-preludes/<p>⭐️ In Rust, language elements are implemented by not only <strong><code>std</code> library</strong> crate but also <strong>compiler</strong> as well. Examples,</p> +<ul> +<li><strong><a href="https://doc.rust-lang.org/std/#primitives" target="_blank" >Primitives</a></strong>: Defined by the compiler and methods are implemented by <code>std</code> library directly on primitives.</li> +<li><strong><a href="https://doc.rust-lang.org/std/#macros" target="_blank" >Standard Macros</a></strong>: Defined by both compiler and <code>std</code></li> +</ul> +<p>The <strong><code>std</code></strong> library has been divided into <strong><a href="https://doc.rust-lang.org/std/#modules" target="_blank" >modules</a></strong>, according to the main areas each covered.</p> +<p>⭐️ While primitives are implemented by the <strong>compiler</strong>, the standard library implements the <strong>most useful methods</strong> directly on the primitive types. But some <strong>rarely useful language elements</strong> of some primitives are stored on relevant <strong><code>std</code> modules</strong>. This is why you can see <code>char</code>, <code>str</code> and integer types on both <a href="https://doc.rust-lang.org/std/#primitives" target="_blank" >primitives</a> and <a href="https://doc.rust-lang.org/std/#modules" target="_blank" ><code>std</code> modules</a>.</p>Structshttps://learning-rust.github.io/docs/structs/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/structs/<ul> +<li>Used to <strong>encapsulate related properties into one unified data type</strong>.</li> +<li>By convention, the name should follow <a href="https://en.wikipedia.org/wiki/Camel_case" target="_blank" ><code>PascalCase</code></a>.</li> +<li>3 variants, +<ul> +<li> +<p>C-like structs: One or more <code>,</code> separated <code>name: value pairs</code> enclosed in <code>{}</code></p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Color</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">red</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">green</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">blue</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div></li> +<li> +<p>Tuple structs: One or more <code>,</code> separated <code>values</code> enclosed in <code>()</code></p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Color</span><span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"> +</span></span></span></code></pre></div></li> +<li> +<p>Unit structs: A struct with no fields/ members</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">struct</span> <span class="nc">Black</span><span class="p">;</span><span class="w"> +</span></span></span></code></pre></div></li> +</ul> +</li> +</ul> +<blockquote> +<p>⭐️ In Rust, data (attributes) and behavior (associated functions and methods) are placed separately. Structs and Enums are used to group related data, and impls and traits are used to add associated and shared behavior to that data.</p>Traitshttps://learning-rust.github.io/docs/traits/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/traits/<p>A trait is a contract that defines a set of behaviors or properties that a type must implement. It can contain associated types, constants, function or method signatures, and overridable default implementations.</p> +<h2 id="definition">Definition</h2> +<h3 id="with-no-associates">With No Associates</h3> +<p>💡 Mostly used to mark a type as having certain properties to allow in certain operations. Known as Marker Traits.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="nb">Sized</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-the-declarations-of-associates">With the Declarations of Associates</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">trait</span><span class="w"> </span><span class="n">Greet</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="no">PREFIX</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span> <span class="kt">str</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">type</span> <span class="nc">Item</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">fn</span> <span class="nf">greet</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">String</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-the-default-implementations-of-associates">With the Default Implementations of Associates</h3> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="k">trait</span><span class="w"> </span><span class="n">Greet</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="no">PREFIX</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span> <span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="s">&#34;Hello&#34;</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">fn</span> <span class="nf">greet</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">String</span> <span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">format!</span><span class="p">(</span><span class="s">&#34;</span><span class="si">{}</span><span class="s">!&#34;</span><span class="p">,</span><span class="w"> </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="bp">Self</span>::<span class="no">PREFIX</span><span class="p">))</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h3 id="with-supertraits">With Supertraits</h3> +<p>A trait must have to be implemented first before implementing the current trait.</p>Unwrap and Expecthttps://learning-rust.github.io/docs/unwrap-and-expect/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/unwrap-and-expect/<h2 id="unwrap">unwrap()</h2> +<ul> +<li>If an <code>Option</code> type has <strong><code>Some</code></strong> value or a <code>Result</code> type has a <strong><code>Ok</code></strong> value, <strong>the value inside them</strong> passes to the next step.</li> +<li>If the <code>Option</code> type has <strong><code>None</code></strong> value or the <code>Result</code> type has <strong><code>Err</code></strong> value, <strong>program panics</strong>; If <code>Err</code>, panics with the error message.</li> +</ul> +<p>The functionality is bit similar to the following codes, which are using <code>match</code> instead <code>unwrap()</code>.</p> +<p>Example with <code>Option</code> and <code>match</code>, before using <code>unwrap()</code></p>Usehttps://learning-rust.github.io/docs/use/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/use/<p>Let&rsquo;s see the main usages of the <code>use</code> keyword.</p> +<h2 id="01-bind-a-full-path-to-a-new-name">01. Bind a full path to a new name</h2> +<p>Mainly <code>use</code> keyword is used to bind a full path of an element to a new name. So the user doesn’t want to repeat the full path each time.</p> +<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-rust" data-lang="rust"><span class="line"><span class="cl"><span class="c1">// -- Initial code without the `use` keyword -- +</span></span></span><span class="line"><span class="cl"><span class="k">mod</span> <span class="nn">phrases</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">mod</span> <span class="nn">greetings</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">&#34;Hello, world!&#34;</span><span class="p">);</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> </span><span class="c1">// Using full path +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// -- Usage of the `use` keyword -- +</span></span></span><span class="line"><span class="cl"><span class="c1">// 01. Create an alias for module +</span></span></span><span class="line"><span class="cl"><span class="k">use</span><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">greetings</span>::<span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 02. Create an alias for module elements +</span></span></span><span class="line"><span class="cl"><span class="k">use</span><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">hello</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="c1">// 03. Customize names with the `as` keyword +</span></span></span><span class="line"><span class="cl"><span class="k">use</span><span class="w"> </span><span class="n">phrases</span>::<span class="n">greetings</span>::<span class="n">hello</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">greet</span><span class="p">;</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="w"> </span><span class="n">greet</span><span class="p">();</span><span class="w"> +</span></span></span><span class="line"><span class="cl"><span class="p">}</span><span class="w"> +</span></span></span></code></pre></div><h2 id="02-import-elements-to-scope">02. Import elements to scope</h2> +<p>Another common usage of <code>use</code> is importing elements to scope. Remember that, this is also a bit similar to creating an alias and using it instead of using the full path.</p>Variable bindings, Constants & Staticshttps://learning-rust.github.io/docs/variable-bindings-constants-and-statics/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/variable-bindings-constants-and-statics/<ul> +<li>Rust is a statically typed language; it checks data types at compile-time. But it doesn’t require you to actually type data types when declaring variable bindings. In that case, the compiler checks the usage and sets a better data type for it.</li> +<li>⭐️ For <strong>constants and statics, we must annotate the data type</strong>.</li> +<li>Types come after a <code>:</code> (colon) sign.</li> +<li>The naming convention for the variable bindings is using the <a href="https://en.wikipedia.org/wiki/Snake_case" target="_blank" ><code>snake_case</code></a>. But, for constants and statics, we should follow the <a href="https://en.wikipedia.org/wiki/Snake_case" target="_blank" ><code>SCREAMING_SNAKE_CASE</code></a>.</li> +</ul> +<blockquote> +<p>💭 In the following examples, we will use <a href="https://learning-rust.github.io/docs/primitive-data-types" >data types</a> like <code>bool</code>, <code>i32</code>, <code>i64</code> and <code>f64</code>. Don&rsquo;t worry about them for now; they&rsquo;ll be discussed later.</p>Vectorshttps://learning-rust.github.io/docs/vectors/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/vectors/<p>If you remember, the array is a fixed-size list of elements, of the same data type. Even with mut, its element count cannot be changed. A vector is <strong>kind of a re-sizable array</strong> but <strong>all elements must be in the same type</strong>.</p> +<blockquote> +<p>💡 <code>Vec&lt;T&gt;</code>: capital “V” as <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html" target="_blank" >it’s a struct</a>.</p> +</blockquote> +<p>It’s a generic type, written as <strong><code>Vec&lt;T&gt;</code></strong>. T can have any type, ex. A vector of i32s is <code>Vec&lt;i32&gt;</code>. Also, Vectors always allocate their data in a dynamically allocated heap.</p>Why Rust?https://learning-rust.github.io/docs/why-rust/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/why-rust/<h2 id="history-of-rust">History of Rust</h2> +<p>Rust was initially designed and developed by former Mozilla employee <strong><a href="https://github.com/graydon" target="_blank" >Graydon Hoare</a></strong> as a personal project. Mozilla began sponsoring the project in 2009 and announced it in 2010. But the first stable release, Rust 1.0, was released on May 15, 2015.</p> +<p>Since Rust 1.0, major updates have been released as <a href="https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/#rust-editions" ><code>Editions</code></a> approximately every three years: Rust 2015 (with the release of Rust 1.0) , Rust 2018, Rust 2021, and Rust 2024, all maintaining backward compatibility.</p>Workspaceshttps://learning-rust.github.io/docs/workspaces/Mon, 01 Jan 0001 00:00:00 +0000https://learning-rust.github.io/docs/workspaces/<p>When the code base is getting larger, you might need to work with <strong>multiple crates on the same project</strong>. Rust supports this via Workspaces. You can <strong>analyze (<code>cargo check</code>), build, run tests or generate docs for all crates</strong> at once by running <code>cargo</code> commands from the project root.</p> +<p>⭐️ When working on multiple crates same time, there is a higher possibility of having shared dependencies on crates. To prevent downloading and compiling the same dependency multiple times, Rust uses a <strong>shared build directory</strong> under the project root, while running <code>cargo build</code> from the project root.</p> \ No newline at end of file diff --git a/docs/manifest.json b/docs/manifest.json new file mode 100644 index 00000000..87493f4f --- /dev/null +++ b/docs/manifest.json @@ -0,0 +1,21 @@ +{ + "name": "Learning Rust", + "short_name": "Learning Rust", + "description": "Rust Programming Language Tutorials for Everyone!", + "start_url": "/?source=pwa", + "display": "standalone", + "icons": [ + { + "src": "/favicon/android-chrome-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/favicon/android-chrome-512x512.png", + "sizes": "512x512", + "type": "image/png" + } + ], + "background_color": "#866ee7", + "theme_color": "#866ee7" +} \ No newline at end of file diff --git a/docs/og.jpg b/docs/og.jpg new file mode 100644 index 00000000..a7e65376 Binary files /dev/null and b/docs/og.jpg differ diff --git a/docs/robots.txt b/docs/robots.txt new file mode 100644 index 00000000..1068142a --- /dev/null +++ b/docs/robots.txt @@ -0,0 +1,41 @@ +User-agent: Googlebot +User-agent: YandexBot +User-agent: baiduspider +User-agent: Applebot +Allow: / + +User-agent: * +Disallow: /docs/borrowing/ +Disallow: /docs/cargo-crates-and-basic-project-structure/ +Disallow: /docs/combinators/ +Disallow: /docs/comments-and-documenting-the-code/ +Disallow: /docs/control-flows/ +Disallow: /docs/crates/ +Disallow: /docs/custom-error-types/ +Disallow: /docs/ +Disallow: /docs/enums/ +Disallow: /docs/error-and-none-propagation/ +Disallow: /docs/functions/ +Disallow: /docs/generics/ +Disallow: /docs/hello-world/ +Disallow: /docs/impls/ +Disallow: /docs/installation/ +Disallow: / +Disallow: /docs/lifetimes/ +Disallow: /docs/modules/ +Disallow: /docs/operators/ +Disallow: /docs/option-and-result/ +Disallow: /docs/overview/ +Disallow: /docs/ownership/ +Disallow: /docs/panicking/ +Disallow: /docs/primitive-data-types/ +Disallow: /docs/smart-compiler/ +Disallow: /docs/std-primitives-and-preludes/ +Disallow: /docs/structs/ +Disallow: /docs/traits/ +Disallow: /docs/unwrap-and-expect/ +Disallow: /docs/use/ +Disallow: /docs/variable-bindings-constants-and-statics/ +Disallow: /docs/vectors/ +Disallow: /docs/why-rust/ +Disallow: /docs/workspaces/ \ No newline at end of file diff --git a/docs/sitemap.xml b/docs/sitemap.xml new file mode 100644 index 00000000..8cafa00b --- /dev/null +++ b/docs/sitemap.xml @@ -0,0 +1 @@ +https://learning-rust.github.io/docs/borrowing/2022-10-17T01:47:29+08:00https://learning-rust.github.io/docs/cargo-crates-and-basic-project-structure/2025-10-13T20:20:39+08:00https://learning-rust.github.io/docs/combinators/2025-10-13T20:20:39+08:00https://learning-rust.github.io/docs/comments-and-documenting-the-code/2025-10-30T20:23:09+08:00https://learning-rust.github.io/docs/control-flows/2025-10-30T20:23:09+08:00https://learning-rust.github.io/docs/crates/2025-10-13T20:20:39+08:00https://learning-rust.github.io/docs/custom-error-types/2023-11-11T20:38:50+08:00https://learning-rust.github.io/docs/2026-03-04T11:37:51+08:00https://learning-rust.github.io/docs/enums/2025-12-09T03:28:34+08:00https://learning-rust.github.io/docs/error-and-none-propagation/2023-11-11T20:38:50+08:00https://learning-rust.github.io/docs/functions/2025-10-30T20:23:09+08:00https://learning-rust.github.io/docs/generics/2025-12-10T03:17:52+08:00https://learning-rust.github.io/docs/hello-world/2025-10-30T20:23:09+08:00https://learning-rust.github.io/docs/impls/2026-01-14T16:01:02+08:00https://learning-rust.github.io/docs/installation/2025-10-13T20:20:39+08:00https://learning-rust.github.io/2026-03-04T11:37:51+08:00https://learning-rust.github.io/docs/lifetimes/2025-10-13T20:20:39+08:00https://learning-rust.github.io/docs/modules/2022-10-17T01:47:29+08:00https://learning-rust.github.io/docs/operators/2025-11-03T21:22:05+08:00https://learning-rust.github.io/docs/option-and-result/2024-02-01T19:02:07+01:00https://learning-rust.github.io/docs/overview/2024-03-10T20:15:12+08:00https://learning-rust.github.io/docs/ownership/2026-02-23T20:27:43+08:00https://learning-rust.github.io/docs/panicking/2023-11-11T20:38:50+08:00https://learning-rust.github.io/docs/primitive-data-types/2025-12-01T20:29:50+08:00https://learning-rust.github.io/docs/smart-compiler/2023-11-11T20:38:50+08:00https://learning-rust.github.io/docs/std-primitives-and-preludes/2022-10-17T01:47:29+08:00https://learning-rust.github.io/docs/structs/2026-01-02T13:09:17+08:00https://learning-rust.github.io/docs/traits/2026-03-04T11:37:51+08:00https://learning-rust.github.io/docs/unwrap-and-expect/2023-11-11T20:38:50+08:00https://learning-rust.github.io/docs/use/2022-10-17T01:47:29+08:00https://learning-rust.github.io/docs/variable-bindings-constants-and-statics/2025-10-30T20:23:09+08:00https://learning-rust.github.io/docs/vectors/2025-12-01T20:29:50+08:00https://learning-rust.github.io/docs/why-rust/2026-01-02T11:44:19+08:00https://learning-rust.github.io/docs/workspaces/2025-10-13T20:20:39+08:00 \ No newline at end of file diff --git a/docs/sw.js b/docs/sw.js new file mode 100644 index 00000000..9c27f9f3 --- /dev/null +++ b/docs/sw.js @@ -0,0 +1,56 @@ +const cacheName = 'learning-rust-{{ now.Format "2006-01-02" }}'; +const staticAssets = [ + './', + './index.html', + './manifest.json', + './docs/**/*', + './favicon/android-chrome-192x192.png', + './favicon/android-chrome-512x512.png', + './favicon/apple-touch-icon.png', + './favicon/favicon.ico', + './favicon/favicon-16x16.png', + './favicon/favicon-32x32.png', + './css/home.min.*.css', + './css/docs.min.*.css', + './js/home.min.*.js', + './js/docs.min.*.js', +]; + +self.addEventListener('install', async e => { + const cache = await caches.open(cacheName); + await cache.addAll(staticAssets); + return self.skipWaiting(); +}); + +self.addEventListener('activate', e => { + self.clients.claim(); +}); + +self.addEventListener('fetch', async e => { + const req = e.request; + const url = new URL(req.url); + + if (url.origin === location.origin) { + e.respondWith(cacheFirst(req)); + } else { + e.respondWith(networkFirst(req)); + } +}); + +async function cacheFirst(req) { + const cache = await caches.open(cacheName); + const cached = await cache.match(req); + return cached || fetch(req); +} + +async function networkFirst(req) { + const cache = await caches.open(cacheName); + try { + const fresh = await fetch(req); + cache.put(req, fresh.clone()); + return fresh; + } catch (e) { + const cached = await cache.match(req); + return cached; + } +} \ No newline at end of file diff --git a/docs/tags/index.xml b/docs/tags/index.xml new file mode 100644 index 00000000..6e03319c --- /dev/null +++ b/docs/tags/index.xml @@ -0,0 +1 @@ +Tags on Learning Rusthttps://learning-rust.github.io/tags/Recent content in Tags on Learning RustHugoen-US \ No newline at end of file