Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Rust

You're reading from   Learning Rust A comprehensive guide to writing Rust applications

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher Packt
ISBN-13 9781785884306
Length 308 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Vesa Kaihlavirta Vesa Kaihlavirta
Author Profile Icon Vesa Kaihlavirta
Vesa Kaihlavirta
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Introducing and Installing Rust FREE CHAPTER 2. Variables 3. Input and Output 4. Conditions, Recursion, and Loops 5. Remember, Remember 6. Creating Your Own Rust Applications 7. Matching and Structures 8. The Rust Application Lifetime 9. Introducing Generics, Impl, and Traits 10. Creating Your Own Crate 11. Concurrency in Rust 12. Now It's Your Turn! 13. The Standard Library 14. Foreign Function Interfaces

Traits and Impl


A very powerful feature of Rust that is commonly seen when dealing with generics is that it is possible to tell the compiler that a particular type will provide certain functionality. This is provided by a special feature known as a trait.

However, to appreciate traits, we first have to look at the impl keyword (short for implement).

Impl

The impl keyword works in a very similar way to a function. The structure of an implementation needs to considered as being closer to a static class (in C#) or as a function within a function:

impl MyImpl
{
  fn reference_name (&self) ... 
}

This would be more for a non-generic type. For generics, the preceding code becomes the following:

impl <T> MyGenericImpl<T>
{
  fn reference_name(&self) ... 
}

Note that the <T> is required to tell the compiler that the impl is for a generic. reference_name is the name used to access the impl function. It can be anything you wish.

Note

An example of impl can be found in 09/impl_example...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image