Practical Rust Projects: Build Serverless, AI, Machine Learning, Embedded, Game, and Web Applications (2nd ed.) 2nd Edition Shing Lyu 2024 Scribd Download
Practical Rust Projects: Build Serverless, AI, Machine Learning, Embedded, Game, and Web Applications (2nd ed.) 2nd Edition Shing Lyu 2024 Scribd Download
com
https://ebookmass.com/product/practical-rust-projects-build-
serverless-ai-machine-learning-embedded-game-and-web-
applications-2nd-ed-2nd-edition-shing-lyu/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmass.com/product/practical-rust-projects-second-edition-
shing-lyu/
ebookmass.com
https://ebookmass.com/product/practical-matlab-deep-learning-a-
projects-based-approach-2nd-edition-michael-paluszek/
ebookmass.com
https://ebookmass.com/product/practical-machine-learning-on-
databricks-1st-edition-debu-sinha/
ebookmass.com
https://ebookmass.com/product/machine-learning-for-healthcare-
applications-sachi-nandan-mohanty/
ebookmass.com
Shing Lyu
Andrew Rzeznik
Practical Rust Projects: Build Serverless, AI, Machine Learning, Embedded, Game,
and Web Applications
Shing Lyu Andrew Rzeznik
Amsterdam, The Netherlands Arlington, MA, USA
iii
Table of Contents
iv
Table of Contents
v
Table of Contents
Chapter 6: Going Serverless with the Amazon AWS Rust SDK����������������������������� 201
6.1 What Are You Building?������������������������������������������������������������������������������������������������������� 202
6.2 What Is AWS Lambda?�������������������������������������������������������������������������������������������������������� 202
6.3 Registering an AWS Account����������������������������������������������������������������������������������������������� 203
6.4 Hello World in Lambda�������������������������������������������������������������������������������������������������������� 204
6.5 The Full Architecture����������������������������������������������������������������������������������������������������������� 210
6.6 Using the AWS Serverless Application Model (AWS SAM)��������������������������������������������������� 211
6.7 Setting Up AWS SAM CLI Credentials���������������������������������������������������������������������������������� 212
6.8 Creating the Catdex Serverless Project������������������������������������������������������������������������������ 212
6.9 Building the Upload API������������������������������������������������������������������������������������������������������� 219
6.10 Building the /cats API����������������������������������������������������������������������������������������������������� 224
6.11 Uploading the Image Using S3 Pre-signed URL���������������������������������������������������������������� 228
6.12 Adding the Frontend���������������������������������������������������������������������������������������������������������� 235
6.13 A Note on Security������������������������������������������������������������������������������������������������������������ 245
6.14 Next Steps������������������������������������������������������������������������������������������������������������������������� 246
6.15 Conclusion������������������������������������������������������������������������������������������������������������������������ 246
vi
Table of Contents
vii
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 385
viii
About the Authors
Shing Lyu is a software engineer who is passionate about
open source software. He’s worked on Rust professionally
at Mozilla on the Firefox (Gecko) and Servo browser engine
project. Currently, he works at Amazon Web Services (AWS)
as a solutions architect. Previously, Shing worked at DAZN,
a sports streaming platform, as a backend developer, with a
focus on AWS and serverless technology. Shing has worked
for other world-famous brands such as Intel. He is also
active in the open source community. Being one of the
founders of the Taiwan Rust community, he loves to share
his enthusiasm for Rust with people.
ix
About the Technical Reviewer
Satej Kumar Sahu works in the role of senior software data
architect at Boeing. He is passionate about technology,
people, and nature. He believes that through technology
focused on sustainability and conscientious decision making
each of us has the power to make this world a better place.
In his free time, he can be seen reading books, playing
basketball, and having fun with friends and family.
xi
Introduction
Almost every programming language has one or more books about it that provide a
deep dive into fundamental syntax, semantics, and functionality. These books are
amazing as both an introduction and a reference. It’s important to be able to dive deep
and fully immerse yourself in a topic to gain a thorough understanding of it. Whenever
I am starting to learn a new language, I look for this kind of foundational text to start my
journey.
After completing this first stage, however, there is a question of where to move next.
Sometimes you’ve learned a language for a very specific purpose, so you focus all your
energies toward using the language for that task. But breadth of learning is also very
important, and can sometimes be harder to find. The best programmers don’t just
know their own specialty, but also have a broad foundation of knowledge along with
background on adjacent subjects. Learning different applications of a language can open
your mind to new ideas both within that language and outside of it. It’s also really fun to
throw together a new project that does something you’ve never done before.
This book was created to provide some of these projects, which will let you take your
core Rust knowledge and apply it to several different areas. Its goal is to expose you to
new ideas and different ways of thinking, and show, not tell, you how Rust can be used.
One of the big reasons I was drawn to Rust as a programming language was how it
embodies the “general purpose” paradigm. If you ask ten different people why they like
the language, you’ll frequently get ten different answers. Some people like it because it’s
low level; it compiles directly to machine code in the same way as C, with all of the fine
control afforded there. However, some people like it because it’s high level, with a strong
type system and advanced macros for working with abstract constructs. Some people
like Rust because it feels like a safer C++, while others learn it as a more performant
alternative to Python. Some people like Rust for its large selection of available packages,
while others like how powerful the core language feels even when the standard library
has been disabled.
xiii
Introduction
A lot of these different views are because Rust exists in a big ecosystem with wildly
different applications, especially for being such a comparatively young language. On
one end, it’s possible to write tiny bare-metal programs whose compiled size can be
measured in hundreds of bytes (not megabytes, not kilobytes, but bytes). On the other
end, people are currently building operating systems, language compilers, and large
distributed systems in Rust. This book was written to share with you a small set of these
interesting applications, and hopefully show you the benefits (and joy) of writing them
in Rust.
A drawback to the approach taken here is that we can’t go into great depth on any
of the topics. A whole book could be written for each single-chapter project we present
here, diving deep into advanced theory, design, and tradeoffs. We chose to instead
focus only on the core essence of each project, providing you with a scaffold that does
something useful and that you can later extend with your own ideas. As any programmer
knows, frequently the hardest parts of a new project are the initial setup, architecture,
and library choices. We’ve tried to provide these components to you as a solid
foundation for your projects, while also trying to include some interesting examples that
touch on important issues in each of the topics.
We hope that this book lets you see the many faces of the Rust programming
language, where it shines, and in some cases the places where a person like you can
make things better. Many programmers feel that the gap between a novice and an expert
is insurmountable, and that the people who build “real” production applications must
have some unique gifts. Hopefully the examples here will help build your confidence
and show you that anyone can build real, practical projects in Rust with a little guidance
and determination.
Source Code
All code used in this book can be found at g ithub.com/apress/practical-rust-
projects-2e.
xiv
CHAPTER 1
1
https://crates.io/categories
1
© Shing Lyu and Andrew Rzeznik 2023
S. Lyu and A. Rzeznik, Practical Rust Projects, https://doi.org/10.1007/978-1-4842-9331-7_1
Chapter 1 Welcome to the World of Rust
• people who already know basic Rust syntax, but want to learn how to
build applications in Rust;
You might have learned Rust out of curiosity. After finishing all the tutorials and
beginner books, you might have been left wondering, “What should I do next? What
can I build with Rust?” This book will walk you through a few different applications of
Rust, which will help you move beyond basic language theory and into building real
applications. Rust has a fascinating design and many interesting language features, but
simply knowing how to write basic algorithms and data structures won’t necessarily
prepare you for building useful applications. We’ve tried to find the most production-
ready but also modern Rust libraries to do the job, so you’ll be able to judge if Rust is
ready for the application you’ve envisioned. If it’s not, you might find opportunities to
contribute back to Rust’s community by improving the existing libraries and frameworks,
or by designing and building new ones.
You might have learned Rust for a specific project, like a CLI tool for work or an open-
source browser engine that happens to use Rust. Once you master Rust for that domain,
it’s beneficial to learn Rust for other domains—say, building a game or a website. This will
open you to new ideas that you can apply to the original domain. For example, by building
a game, you’ll know how game engine designers organize their code to make it decoupled
and easy to maintain, while also being very performant. You may never build a game for
work, but that knowledge might influence the architecture of your next project. As another
example, learning how to cross-compile code to a Raspberry Pi might help you understand
how compiling to WebAssembly works. This book aims to take you through a tour of
various applications of Rust like this. You’ll learn what their application programming
interfaces (APIs)2 look like and how they organize their code and architecture.
2
We use the term “API” in a general sense. This includes the functions, structs, and command-line
tools exposed by each library or framework.
2
Discovering Diverse Content Through
Random Scribd Documents
With something vital in her, like those flowers
That on our desolate steppes outlast the year.
Resembles you in some things. It was that
First made us friends. I do her justice, see!
For we were friends in that smooth surface way
We Russians have imported out of France.
Alas! from what a blue and tranquil heaven
This bolt fell on me! After these two years,
My suit with Ossip Leminoff at end,
The old wrong righted, the estates restored,
And my promotion, with the ink not dry!
For those fairies which neglected me at birth
Seemed now to lavish all good gifts on me—
Gold roubles, office, sudden dearest friends.
The whole world smiled; then, as I stooped to taste
The sweetest cup, freak dashed it from my lips.
This very night—just think, this very night—
I planned to come and beg of you the alms
I dared not ask for in my poverty.
I thought me poor then. How stript am I now!
There’s not a ragged mendicant one meets
Along the Nevski Prospekt but has leave to tell his love,
And I have not that right!
Pauline Pavlovna, why do you stand there
Stark as a statue, with no word to say?
She (very slowly). Well, then—I love you. I may tell you so
This once, ... and then forever hold my peace.
We cannot stay here longer unobserved.
No—do not touch me! but stand further off, and
Seem to laugh, as if we jested—eyes,
Eyes, everywhere! Now turn your face away....
I love you.
He. Pauline, I have three things to choose from; you shall choose.
This marriage, or Siberia, or France.
The first means hell; the second, purgatory;
The third—with you—were nothing less than heaven!
He. Not a ray of hope! His mind is set on this with that insistence
Which seems to seize on all match-making folk—
The fancy bites them, and they straight go mad.
GUNGA DIN
By Rudyard Kipling
’E carried me away
To where a dooli lay,
An’ a bullet came and drilled the beggar clean.
’E put me safe inside,
An’ just before ’e died:
“I ’ope you like your drink,” sez Gunga Din.
So I’ll meet ’im later on
At the place where ’e is gone—
Where it’s always double drill and no canteen;
’E’ll be squattin’ on the coals,
Givin’ drink to poor damned souls,
An’ I’ll get a swig in hell from Gunga Din!
Yes, Din! Din! Din!
You Lazarushian-leathern Gunga Din!
Though I’ve belted you and flayed you,
By the livin’ God that made you,
You’re a better man than I am, Gunga Din!
As we paced along
Upon the giddy footing of the hatches,
Methought the Gloucester stumbled; and, in falling,
Struck me, that thought to stay him, overboard,
Into the tumbling billows of the main.
Lord! Lord! methought, what pain it was to drown!
What dreadful noise of waters in mine ears!
What ugly sights of death within mine eyes!
The blacksmith raised his hammer, and rushed into the street,
His ’prentice boys behind him, the ruthless foe to meet;—
High on the breach of Limerick with dauntless hearts they stood,
Where bomb-shells burst, and shot fell thick, and redly ran the blood.
“Now look you, brown-haired Moran; and mark you, swarthy Ned,
This day we’ll prove the thickness of many a Dutchman’s head!
Hurrah! upon their bloody path, they’re mounting gallantly;
And now the first that tops the breach, leave him to this and me.”
He rushed upon the flying ranks; his hammer ne’er was slack,
For in thro’ blood and bone it crashed, thro’ helmet and thro’ jack;
He’s ta’en a Holland captain beside the red pontoon,
And “Wait you here,” he boldly cries; “I’ll send you back full soon!
The blacksmith sought his smithy and blew his bellows strong;
He shod the steed of Sarsfield, but o’er it sang no song;
“Ochone! my boys are dead!” he cried; “their loss I’ll long deplore,
But comfort’s in my heart, their graves are red with foreign gore.”
Up, comrades, up, the bugle peals the note of war’s alarms,
And the cry is ringing sternly round, that calls the land to arms;
Adieu, adieu, fair land of France, where the vine of Brennus reigns;
We go where the blooming laurels grow, on the bright Italian plains.
Advance! advance! brave sons of France, before the startled world;
For France, once more, her tricolor in triumph hath unfurled.
Our eagles shall fly ’neath many a sky, with a halo round their way
Where History flings, on their flashing wings, the light of Glory’s ray;
And we shall bear them proudly on, through many a mighty fray,
That shall win old nations back to life, in the glorious coming day.
Then advance, advance, ye sons of France, before the startled
world,
For France, once more, her tricolor in triumph hath unfurled.
See the Briton, pale, as he dons his mail, for the coming conflict
shock,
And before his eyes, see the phantom rise, of the Chief on Helena’s
rock;
In foreboding fears, already he hears through palace and mart anew,
Our avenging shout, o’er the battle rout—remember Waterloo!
Then advance, advance, ye sons of France, before the startled
world,
For France, once more, her tricolor in triumph hath unfurled.
And, hark, a wail from our kindred Gael, comes floating from the
West—
That gallant race, whose chosen place was ever our battle’s crest;
Now is the day we can repay the generous debt we owe
To Irish blood, that freely flowed to conquer France’s foe.
Then advance, advance, ye sons of France, before the startled
world,
For France, once more, her tricolor in triumph hath unfurled.
Old Tricolor, as in days of yore, you shall wave o’er vanquished
kings,
And your folds shall fly ’neath an English sky, on Victory’s crimson
wings;
And Europe’s shout shall in joy ring out, hailing freedom in thy track,
When our task is done, and we bear thee on, to France with glory
back.
Then advance, advance, ye sons of France, before the startled
world,
For France, once more, her tricolor in triumph hath unfurled.
I know no fears, but the mist of years that has gathered round my
track
For a moment clears, and my youth’s compeers again to my side
come back;
And the tall ships reel o’er their iron keel, as we sweep down on the
foe,
Like a giant’s form amid the storm, where the mighty tempests blow.
My realm and grave the northern wave, where the tempest’s voice
will sing
My death-song loud, where flame shall shroud the ocean’s warrior-
king,
Whilst heroes wait at Valhalla’s gate to proudly welcome me.
For my race is run, my errand done. Receive thy Chief, O Sea!
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com