Skip to content

Commit

Permalink
Post on "From" trait in Rust.
Browse files Browse the repository at this point in the history
  • Loading branch information
Graham authored and Graham committed Sep 28, 2024
1 parent c4424a9 commit 1bca91e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions content/post/2024-09-28-ImplFrom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Implement "From" Trait
subtitle: Learning Rust
date: 2024-09-28
tags: [
"programming",
"code",
"rust",
]
---

# Implement "From" trait...

Basic implementation of From trait to create "MyStruct" from<i32>
```
struct MyStruct{
word: String,
number: i32
}
impl From<i32> for MyStruct{
fn from(num: i32) -> MyStruct {
MyStruct {
word: "notset".to_string(),
number: num
}
}
}
fn main() {
let m = MyStruct::from(25);
println!("Struct made: word={}, number={}.", m.word, m.number);
}
```

## Output of above:

```
Struct made: word=notset, number=25.
```

0 comments on commit 1bca91e

Please sign in to comment.