Hands-On	Data	Structures	and	Algorithms	with	Rust
	
	
	
	
Learn	programming	techniques	to	build	effective,	maintainable,	and	readable	code	in	Rust	2018
	
	
	
	
	
	
	
	
	
	
	
Claus	Matzinger
	
	
	
	
	
	
	
	
	
	
BIRMINGHAM	-	MUMBAI
Hands-On	Data	Structures	and
Algorithms	with	Rust
Copyright	©	2019	Packt	Publishing
All	rights	reserved.	No	part	of	this	book	may	be	reproduced,	stored	in	a	retrieval	system,	or	transmitted	in	any	form	or	by	any	means,	without	the
prior	written	permission	of	the	publisher,	except	in	the	case	of	brief	quotations	embedded	in	critical	articles	or	reviews.
Every	effort	has	been	made	in	the	preparation	of	this	book	to	ensure	the	accuracy	of	the	information	presented.	However,	the	information
contained	in	this	book	is	sold	without	warranty,	either	express	or	implied.	Neither	the	author(s),	nor	Packt	Publishing	or	its	dealers	and
distributors,	will	be	held	liable	for	any	damages	caused	or	alleged	to	have	been	caused	directly	or	indirectly	by	this	book.
Packt	Publishing	has	endeavored	to	provide	trademark	information	about	all	of	the	companies	and	products	mentioned	in	this	book	by	the
appropriate	use	of	capitals.	However,	Packt	Publishing	cannot	guarantee	the	accuracy	of	this	information.
Commissioning	Editor:	Richa	Tripathi
Acquisition	Editor:	Shahnish	Khan
Content	Development	Editor:	Zeeyan	Pinheiro
Technical	Editor:	Romy	Dias
Copy	Editor:	Safis	Editing
Project	Coordinator:	Vaidehi	Sawant
Proofreader:	Safis	Editing
Indexer:	Priyanka	Dhadke
Graphics:	Alishon	Mendonsa
Production	Coordinator:	Tom	Scaria
First	published:	January	2019
Production	reference:	1230119
Published	by	Packt	Publishing	Ltd.
Livery	Place
35	Livery	Street
Birmingham
B3	2PB,	UK.
ISBN	978-1-78899-552-8
www.packtpub.com
	
mapt.io
Mapt	is	an	online	digital	library	that	gives	you	full	access	to	over	5,000	books	and	videos,	as	well	as
industry	leading	tools	to	help	you	plan	your	personal	development	and	advance	your	career.	For
more	information,	please	visit	our	website.
Why	subscribe?
Spend	less	time	learning	and	more	time	coding	with	practical	eBooks	and	Videos	from	over
4,000	industry	professionals
Improve	your	learning	with	Skill	Plans	built	especially	for	you
Get	a	free	eBook	or	video	every	month
Mapt	is	fully	searchable
Copy	and	paste,	print,	and	bookmark	content
Packt.com
Did	you	know	that	Packt	offers	eBook	versions	of	every	book	published,	with	PDF	and	ePub	files
available?	You	can	upgrade	to	the	eBook	version	at	www.packt.com	and	as	a	print	book	customer,	you	are
entitled	to	a	discount	on	the	eBook	copy.	Get	in	touch	with	us	at	customercare@packtpub.com	for	more	details.
At	www.packt.com,	you	can	also	read	a	collection	of	free	technical	articles,	sign	up	for	a	range	of	free
newsletters,	and	receive	exclusive	discounts	and	offers	on	Packt	books	and	eBooks.	
Foreword
Rust	is	not	an	easy	language	to	learn.	Ask	why,	and	you'll	hear	that	Rust	was	designed	to	solve
almost	any	complex	problem	in	system	programming,	a	complicated	domain	to	begin	with.	It	was
designed	to	do	it	safely,	to	be	incredibly	fast,	and	be	very	strict;	"ease	of	use"	is	a	necessary
sacrifice.	Rust	reads	like	any	other	imperative	language,	but	it	incorporates	a	number	of	special
concepts	that	ask	you	to	think	through	your	problems	in	greater	depth	and	with	a	different	spin
than	you're	used	to.	It's	brutally	honest	about	the	complicated	parts	a	system	language	has	to
address.
Those	are	the	typical	reasons	cited	for	why	Rust	is	hard.	The	more	honest	answer	is	that	those
people	may	not	have	the	right	teacher.
I	met	Claus	at	my	first	event	as	an	open	source	developer	for	Microsoft.	He	had	joined	just	a	few
months	before,	and	could	show	me	the	ropes.	It	didn't	occur	to	me	until	a	few	weeks	later	that,	as
his	manager,	I	was	supposed	to	be	teaching	him!	I've	discovered	that	this	is	a	common	situation	for
Claus:	he	falls	naturally	into	a	teaching	role.	Not	a	lecturing	bore,	either—the	kind	of	teaching
where	the	student	doesn't	realize	that's	what's	happening	until	they	find	themselves	using	newly
acquired	knowledge.	We've	long	since	moved	into	other	roles,	but	I've	seen	the	pattern	repeated
over	and	over	again.
Early	in	his	career	as	an	open	source	developer,	Claus	found	himself	diving	deep	into
documentation.	And	fair	enough:	it's	often	the	most	important	part	of	a	project!	"Just	three	lines,"
he	said	to	me	once.	"I	just	lost	a	whole	day	of	my	life	because	someone	didn't	bother	to	write	three
lines	of	good	documentation.	I	can	fix	that."
Claus's	background	was	in	academic	software	development,	but	in	his	professional	life,	he	has
rejected	the	dry,	abstract	computer	science	theory	often	taught	in	that	environment.	He	is	one	of
those	rare	developers	who	cares	deeply	about	making	this	easy	for	people	to	understand.	It's
important	that	it	makes	sense,	it's	important	that	it	looks	nice,	it's	important	that	it's	easy	to	follow
—and	how	to	make	it	that	way	is	intuitive	to	him.	I	think	it	honestly	doesn't	occur	to	him	that	other
people	struggle	to	explain	things	the	way	he	does	naturally.
One	of	the	aspects	of	this	book	that	I	appreciated	the	most	when	reading	it	is	the	balance	Claus
strikes.	It	stays	focused	on	the	teaching	goal	without	getting	sidetracked	by	more	technical	detail
than	is	required.	We	all	know	the	feeling	of	reading	that	kind	of	documentation—the	kind	that
demands	to	be	skimmed.	Most	readers,	including	myself,	are	simply	confused	by	too	much	theory	or
detail	at	the	outset.	As	Claus	puts	it,	"most	teachers	make	it	sound	like	something	really	fancy	is
going	on,	but,	in	reality,	it's	quite	simple."	
This	practical	approach	has	made	Claus	an	in-demand	speaker,	community	member,	and	contributor
in	the	Rust	world.	This	book	is	his	next	step	into	teaching	for	a	broader	audience,	and	I'm	excited	to
see	its	impact.
You've	chosen	a	great	teacher!	Rust	is	difficult	to	learn,	but	it	doesn't	have	to	be.	Just	ask	Claus.
	
Campbell	Vertesi
Principal	Software	Engineer	Manager
twitter:	@ohthehugemanatee
ohthehugemanatee.org