-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vineel Kovvuri <[email protected]>
- Loading branch information
1 parent
9b725d1
commit deb4a32
Showing
51 changed files
with
424 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: "Rust: From and Into trait - Part 2" | ||
date: 2025-01-14 23:33:54 | ||
tags: ['Rust'] | ||
--- | ||
|
||
## Rust: From and Into trait usage | ||
|
||
Knowing how Rust's `From` and `Into` traits work is one part of the story, but | ||
being able to use them efficiently is another. In this code sample, we will | ||
explore how to define functions or methods that accept parameters which can be | ||
converted from and into the expected types. | ||
|
||
In this code, we have two functions, `increment()` and `increment2()`, and we | ||
will see how they work with the `From` trait implemented for the `Number` type. | ||
|
||
```rust | ||
struct Number(u64); | ||
|
||
fn increment(n: Number) -> Number { | ||
Number(n.0 + 1) | ||
} | ||
|
||
fn increment2<T>(n: T) -> Number | ||
where | ||
T: Into<Number>, | ||
{ | ||
let number = n.into(); // Conversion is done by the method itself | ||
Number(number.0 + 1) | ||
} | ||
|
||
impl From<u64> for Number { | ||
fn from(value: u64) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
fn main() { | ||
// Just because we have implemented the From<u64> do not make the conversion | ||
// from 10 to Number implicit. Below line do not compile. | ||
let num = increment(10); | ||
// You have to explicitly request the conversion by calling .into() | ||
let num = increment(10.into()); | ||
// Or even better, make the increment2 method accept any type that can be | ||
// converted into a Number. Since u64 can be converted into Number (as it | ||
// implements From<u64>), increment2() can call the .into() method itself to | ||
// perform the conversion. At the call site, we simply pass the u64 as a | ||
// parameter. | ||
let num = increment2(10); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
docs/blog/a-newbies-introduction-to-compilation-process-and-reverse-engineering/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<!DOCTYPE html> | ||
<html lang="en-us"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Rust: From and Into trait - Part 2 | Vineel Kovvuri</title> | ||
<link rel="stylesheet" href="/css/style.css" /> | ||
<link rel="stylesheet" href="/css/fonts.css" /> | ||
<link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" rel="stylesheet"> | ||
|
||
</head> | ||
|
||
<body> | ||
<nav> | ||
<ul class="menu"> | ||
|
||
<li><a href="/">Home</a></li> | ||
|
||
<li><a href="/blog/">Blog</a></li> | ||
|
||
<li><a href="/presentations/">Presentations</a></li> | ||
|
||
<li><a href="/index.xml">Subscribe</a></li> | ||
|
||
</ul> | ||
<hr/> | ||
</nav> | ||
|
||
<div class="article-meta"> | ||
<h1><span class="title">Rust: From and Into trait - Part 2</span></h1> | ||
|
||
<h2 class="date">2025/01/14</h2> | ||
</div> | ||
|
||
<main> | ||
<h2 id="rust-from-and-into-trait-usage">Rust: From and Into trait usage</h2> | ||
<p>Knowing how Rust’s <code>From</code> and <code>Into</code> traits work is one part of the story, but | ||
being able to use them efficiently is another. In this code sample, we will | ||
explore how to define functions or methods that accept parameters which can be | ||
converted from and into the expected types.</p> | ||
<p>In this code, we have two functions, <code>increment()</code> and <code>increment2()</code>, and we | ||
will see how they work with the <code>From</code> trait implemented for the <code>Number</code> type.</p> | ||
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-rust" data-lang="rust"><span style="display:flex;"><span><span style="color:#080;font-weight:bold">struct</span> <span style="color:#b06;font-weight:bold">Number</span>(<span style="color:#888;font-weight:bold">u64</span>);<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">increment</span>(n: <span style="color:#b06;font-weight:bold">Number</span>)<span style="color:#bbb"> </span>-> <span style="color:#b06;font-weight:bold">Number</span><span style="color:#bbb"> </span>{<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>Number(n.<span style="color:#00d;font-weight:bold">0</span><span style="color:#bbb"> </span>+<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">1</span>)<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">increment2</span><T>(n: <span style="color:#b06;font-weight:bold">T</span>)<span style="color:#bbb"> </span>-> <span style="color:#b06;font-weight:bold">Number</span><span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">where</span><span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>T: <span style="color:#038">Into</span><Number>,<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>{<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>number<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>n.into();<span style="color:#bbb"> </span><span style="color:#888">// Conversion is done by the method itself | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span>Number(number.<span style="color:#00d;font-weight:bold">0</span><span style="color:#bbb"> </span>+<span style="color:#bbb"> </span><span style="color:#00d;font-weight:bold">1</span>)<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">impl</span><span style="color:#bbb"> </span><span style="color:#038">From</span><<span style="color:#888;font-weight:bold">u64</span>><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">for</span><span style="color:#bbb"> </span>Number<span style="color:#bbb"> </span>{<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">from</span>(value: <span style="color:#888;font-weight:bold">u64</span>)<span style="color:#bbb"> </span>-> <span style="color:#b06;font-weight:bold">Self</span><span style="color:#bbb"> </span>{<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>Self(value)<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span>}<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span><span style="color:#080;font-weight:bold">fn</span> <span style="color:#06b;font-weight:bold">main</span>()<span style="color:#bbb"> </span>{<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#888">// Just because we have implemented the From<u64> do not make the conversion | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// from 10 to Number implicit. Below line do not compile. | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>num<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>increment(<span style="color:#00d;font-weight:bold">10</span>);<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#888">// You have to explicitly request the conversion by calling .into() | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>num<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>increment(<span style="color:#00d;font-weight:bold">10.</span>into());<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"> </span><span style="color:#888">// Or even better, make the increment2 method accept any type that can be | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// converted into a Number. Since u64 can be converted into Number (as it | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// implements From<u64>), increment2() can call the .into() method itself to | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// perform the conversion. At the call site, we simply pass the u64 as a | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#888">// parameter. | ||
</span></span></span><span style="display:flex;"><span><span style="color:#888"></span><span style="color:#bbb"> </span><span style="color:#080;font-weight:bold">let</span><span style="color:#bbb"> </span>num<span style="color:#bbb"> </span>=<span style="color:#bbb"> </span>increment2(<span style="color:#00d;font-weight:bold">10</span>);<span style="color:#bbb"> | ||
</span></span></span><span style="display:flex;"><span><span style="color:#bbb"></span>}<span style="color:#bbb"> | ||
</span></span></span></code></pre></div> | ||
</main> | ||
|
||
<footer> | ||
|
||
<script defer src="/js/center-img.js"></script> | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<hr/> | ||
© Vineel Kumar Reddy Kovvuri 2017 – 2025 ❤️ <a href="https://xmin.yihui.org/">Hugo Xmin</a> | ||
|
||
</footer> | ||
</body> | ||
</html> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.