►
Description
One of the secret ingredients in many successful, big code bases is that they found a good level of abstraction. One helpful technique is to write declarative code: You describe "what" you want to do, but don't mix it with the "how". While this is not specific to Rust, the language and its ecosystem help us write declarative code. This talk will go over some examples, covering famous crates and APIs as well as more obscure ones, to then dive into ways to use this to your advantage in your own code.
https://rome.rustfest.eu/sessions/declarative-programming-in-rust
https://media.ccc.de/v/rustfest-rome-5-declarative-programming-in-rust
A
Yeah
today
I'm
here
to
talk
to
you
about
the
cloud
of
programming
and
after
Eric's,
perfect
introduction,
I
really
don't
have
to
introduce
myself
anymore.
It
seems,
and
but
I've
been
working
with
rust
since
early
2014
and
I've
been
loving
every
minute
of
it
right
now,
I'm
mostly
concerned
with
making
CLI
tools
in
rust,
really
amazing,
and
also,
if
you
happen
to
be
in
Cologne,
you
should
come
to
Ronnie
down.
You
can
find
me
on
the
on
the
internet
on
deuce
addresses
and
with
that,
let's
start
at
the
beginning,
the
very
very
beginning.
A
Let's
think
about
that,
for
a
minute
now:
okay,
fine!
So
to
make
a
program
work,
you
basically
have
to
tell
the
computer
what
to
do
step
by
step
step
by
very
tiny
step.
So
this
is
imperative.
This
is
literally
called
imperative
programming
and
if
you
do
enough
of
this,
it's
very
hard
to
reason
about
the
general
behavior
of
your
program,
but
you
might
be
saying
where
this
rust
fest
a
lot
of
assembly
camp.
So
what
are
you
even
talking
about
if
you're
here
for
assembly?
A
This
is
not
the
talk
for
you
I'm,
so
sorry,
but
the
common
theme
was
my
talks,
said
I'll
at
some
point
drift
off
and
talk
about
what
maintainable
code
looks
like
and
that's
always
having
maintainable
code,
it's
in
hiring
demand,
but
it's
also
very
hard
to
get
there.
So
the
question
you
need
to
ask
yourself:
this
is
my
code
maintainable,
and
this
is
a
good
question
and
I
have
some
more
questions.
A
These
are
very
generic
questions,
I
know
and
they
apply
to
basically
every
piece
of
code
you've
ever
written
I
think
so
what
does
it
actually
have
to
do
with
this
talk,
because
the
talk
is
declarative
programming,
so
let's
define
this
and
as
always,
when
I
need
to
find
the
definition
of
something
I
go
to
the
Kapiti
I
write
wikipedia
tells
me
that
the
cloud
of
program
is
often
defined
as
any
style
of
programming.
That
is
not
in
person.
A
A
You
abstract,
over
control
flow.
That
means
you
have
in
your
editor,
with
syntax,
highlighting
less
of
those
colorful
key
words
and
more
expressive
function,
names
and,
in
the
end,
you
hope
to
be
able
to
compose
your
application
application
from
smaller
parts.
Okay,
okay,
I'm,
fine,
I
know
what
you're
saying
can
get
to
the
point.
What
does
it
actually
have
to
do
with
rust?
The
super
abstract
like
okay?
A
How
do
I
apply
this,
and
this
is
very
good
question,
because
this
is
actually
about
introducing
abstractions
and
if
they're
right,
you
end
up
as
code,
that's
easier
to
reason
about
and
declarative
code
in
rust,
okay,
fine!
So,
as
the
cloud
of
code
is
ever
saying,
but
imperative
code,
apparently
and
rust
is
totally
able
to
give
you
all
the
emotive
needs
you
ever
need
to
have.
A
Instead,
russ
tries
to
give
you
high
performance
code
with
high
abstraction,
and
this
talk
will
be
by
example,
because
it's
a
short
talk
and
I've
also
been
talking
for
six
minutes
already,
and
I
can't
possibly
give
you
a
recipe
to
turn
on
your
code
into
better
code,
at
least
not
in
this
talk,
and
I
hope
to
give
you
some
examples
in
its
value.
So,
let's
start
with
something
simple
and
that
was
loose
in
every
piece
of
code.
I've
ever
touched
at
some
points.
A
This
is
simply
enough.
It's
actually
easy
to
read
and
if
you're
used
to
the
style,
it's
very
very
common
and
you
wouldn't
question
it,
but
in
the
real
world
we
have
gonna
have
some
problems.
For
example,
there
are
new
requirements,
it
just
happened
to
turn
up
one
day.
Imagine
you're
going
into
the
office
Monday
morning,
and
your
boss
tells
you
ok,
you've
been
doing
this
code,
it's
fine,
it's
working,
but
now
you
need
to
have
something
that
finds
the
fifth
five.
The
five
elements
that
end
with
em.
A
So
you're
gonna
extend
this
piece
of
code
and
you
add
a
mutable
result
to
it
and
to
push
items
into
it,
and
also
you
try
to
only
go
up
to
five
elements.
So
this,
if
is
pretty
much
twice
the
size
and
in
the
end
you
can
return
the
result.
Okay,
this
is
still
easy
to
reason
about.
It's
count
that
fits
on
a
slide.
So
I'd
say
it's
fine,
you
can
read
it
and
you
know
what's
going
on,
but
there
are
some
nuances.
A
A
So
for
the
live
stream,
he
pointed
out
that
I'm
taking
six
elements
because
I'm
not
able
to
count
but
like
I,
said
Monday
morning.
This
is
a
great
example
of
how
iterators
can
save
me,
because
I
don't
have
to
count.
You
can
just
like
take
five
elements
and
it's
good
to
go.
Yeah,
nice,
okay,
okay,
wasn't
actually
expecting
that
good.
But
what
what
have
we
done?
In
general,
we've
abstracted
over
this
control
flow
of
looping
and
conditioning
and
basically
just
create
our
intent
on
filtering
and
checking
five
elements.
A
Also
note
that
this
is
an
abstraction
that
is
pretty
pretty
powerful.
I
have
omitted
to
type
annotations
for
one
reason
specifically,
and
that
is
I.
Don't
care
that
this
is
a
vector
and
I
don't
care
what
I'm
collecting
into
this
could
be
hash
sets
I,
don't
mind.
It's
super
generic
actually,
and
this
makes
sense
if
you
have
a
chain
of
iterator
calls
and
it's
all
good,
but
it
also
makes
sense
if
you
are
looking
for
just
one
condition
in
an
iteration.
For
example,
there
is
an
any
method
that
you
can
use
to
just
check:
hey.
A
Is
there
any
element
in
my
list
and
you
don't
have
to
write
a
for
loop
and
you
don't
have
to
write
a
condition
in
it?
You
can
just
say
any,
because
what
you
want
is
subjective,
selves
and
the
element,
it's
very,
very
expressive.
Okay,
that's
it
for
iterators.
Let's
look
at
another
example,
and
this
is
about
pausing
Jason,
because
Jason
is
a
very
simple
format.
You
could
say:
okay,
I,
don't
have
to
do
any
complicated
things.
A
Maybe
I
don't
know
so
you
know
there
are
30
Jason
and
you
know
it
has
a
value
type,
and
you
know
if
I
have
a
string
that
looks
like
Jason
I
hope
this
is
formatted
correctly.
I
can
get
a
value
out
of
it
probably,
and
if
there
is
some
value
a
that
happens
to
be
a
number
I
can
print
it.
Okay,
easy
enough,
but
next
Monday
arrives
next
requirements
arrive
and
actually
yes,
this
structure
actually
has
three
fields
and
we
need
to
deal
with
this
and,
of
course
this
is
rust.
A
We
know
we
can
write
data
types,
so
we
write
our
structure
called
response
and
say
this
has
three
fields,
and
so
we
continue
to
write
our
code,
as
we've
been
doing
it
for
the
last
week
and
the
difference
is
that
it
got
a
bit
longer
yeah
and
it's
basically
all
the
same.
The
only
thing
that
changes
is
this
character,
and
then
we
have
our
response
easy
enough
right,
but
a
bit
boring
okay.
This
looks
like
it's
pretty
pretty
repetitive
and
not
specific
to
the
actual
problem
we
have.
That
is
dealing
with
our
response.
A
Russ
gives
us
a
very
neat
feature,
and
that
is
procedural,
macros
and
especially
derive
macros.
So,
as
you
may
have
seen
in
Sirte
code,
you
can
just
say
derive
to
serialize,
and
all
this
boilerplate
codes
is
a
repetitive
piece
of
code
that
we've
just
seen
gets
generated,
and
in
fact
this
is
same
as
a
loop,
also
faster,
because
there
is
no
value
that
needs
to
be
allocated
and
filled
with
generic
JSON
data,
and
it
even
has
better
errors
too.
A
A
You
may
have
seen
this
pattern
and
let
I'm
now
going
to
tell
you
that
there
are
new
requirements
and
two
requirement
is
that
this
is
not
how
command
line
tools
should
work.
They
should
also
take
flags
like
OH,
specify
the
output
file,
but
don't
worry,
there's
a
crate
for
that.
It's
called
get
out
and
it
allows
you
to
write
some
easy
enough
code,
so
you
can
specify
an
opt,
opt
whatever.
That
is
not
an
option.
I
don't
know,
but
it's
on
opt
and
it's
called
oh
and
it
sets
your
output
file
name.
A
This
is
from
the
documentation,
I'm
not
kidding,
and
it's
ok.
It
works
for
this
specific
case
and
it's
not
that
much
code
I
teased
a
bit
with
the
scrolling.
But
it's
it's
ok.
You
can
do
this,
so
we
may
have
to
ask
ourselves
how
much
do
you
need
to
read?
It's
ok,
how
much
you
need
to
write
to
make
a
change.
Actually,
this
right,
get-ups
has
nice
abstraction.
You
can
write,
many
ourselves
up,
tops
and
a
opt
thingies,
and
you
can
basically
build
up
your
command
line
to
it.
A
But
how
easy
is
it
to
reason
about?
What's
actually
happening
there?
I,
don't
know
it's
a
bit:
it's
not
very
expressive.
But,
okay,
let's
look
at
the
NASA
crate
that
also
implements
an
abstraction
over
command
line.
Arguments
which
is
called
clap
and
you
may
have
heard
of
it-
it
gives
you
the
possibility
to
write
down
what
your
application
should
look
like.
Basically,
it's
there
is
a
type
hood
app,
that's
a
type
called
arc
and
you
can
compose
an
app
with
arguments.
A
This
also
includes
documentation
and
it's
very
very
expressive,
because
all
this
methods
allow
you
to
express
precisely
what
you
want
your
application
to
look
like
very
nice.
If
you
run
this
with
help,
you
get
a
nice
help
message.
If
you
run
it
with
version,
you
get
a
version
number.
This
is
abstracted
right
for
you,
because
it's
always
assignments.
A
Interesting,
maybe
we
can
do
something
similar
were
studied
it
and
yeah.
We
can
because
isn't
commands
and
arguments
just
to
take
the
structure.
In
fact,
those
library
called
struct
up,
which
you
may
have
also
heard
of,
and
it
allows
you
to
describe
your
command-line
arguments
as
a
structure
and
derive
basically
all
the
code
we've
seen
before,
including
using
the
documents
for
health
messages.
A
Ok,
ok,
I've
talked
quite
a
lot
about
procedural
macros
and
derive
macros,
but
I
also
want
to
touch
on
another
subject,
because
this
is
not
to
talk
about
macros,
even
though
it
may
seem
like
it.
This
is
a
talk
about
abstraction,
and
this
requires
me
to
use
the
scary-looking
headline,
I
guess,
and
to
make
this
crescendo
of
increasing
complexity,
an
example
it's
usable
and
what
I'm
trying
to
get
at
is
we
write
generic
functions
and
our
generic
functions
can
use
concrete
types
to
declare
what
to
do
the
walls.
A
A
very
nice
example,
I've
recently
rediscovered
so
to
say,
is
in
web
frameworks.
I
know
there
are
many
web
frameworks
also
for
us,
but
let's
imagine
there
is
a
very,
very
generic
one
and
a
very
simple
one,
not
generic
in
the
scans
and
it's
a
framework
where
you
specify
a
function
that
takes
a
request
and
returns
a
response
simply
enough
and
our
request
happens
to
contain
a
body
of
JSON
data
and
we
want
to
get
in
name
filled
out.
A
This
looks
like
basically
the
same
code
we've
seen
in
the
earlier
example,
except
a
few
also
choosing
the
name.
Nobody
if
there
is
nobody
okay,
so
this
is
actually
already
quite
good,
but
you
might
be
wondering
if
we've
previously
managed
to
get
rid
of
this
rising
value.
May
we
can
get
rid
of
it
in
this
case
too,
and
actually
we
can
go
one
step
further,
because
C
Rica's
body
doesn't
have
to
be
tracing
for
metal
at
all
and
that
we
are
taking
the
Rica's
body
and
dealing
with
it
as
if
it
were.
A
Jason
is
something
that
we
spent
two
lines
about
and
actually
maybe
also
a
third
line,
but
we
don't
have
to
because
our
generic
function
could
not
only
take
a
request,
but
maybe
also
some
other
type.
That
tells
us
what
we
actually
want
from
our
request,
and
the
idea
is
that
this
Jason
type
here
is
a
type
that
implements,
for
example,
a
trade
called
from
request
and
when
our
framework
calls
it,
it
gives
it
a
request
and
the
type
size.
A
No
I'm
not
going
to
touch
on
that.
So
sorry,
let
let's
talk
about
what
magic
is
actually
in
the
scientific
sense,
of
course,
and
let's
define
magic
as
code
whose
behavior
is
hard
to
predict
or
to
remember,
because
at
least
I
am
very
forgetful
like.
If
you
show
me
code
and
say:
I
have
written
this
like
half
a
year
ago.
I
will
have
no
idea
what
this
is
about.
So
maybe
it's
also
a
good
idea
to
write
code.
That
is
so
expressive
that
you
don't
have
to
remember
it
to
make
sense.
A
A
This
is
a
quote
also
very
generic
code
and
there
are
programming
language
that
say:
okay,
it's
a
good
idea
to
have
generics,
but
actually
not
in
our
use
case
and
there's
a
very,
very
solid
argument,
because
if
you
want
to
have
correctable
code,
that
is
easy
to
read
that
may
not
be
as
concise
as
you
want
it
to
be,
but
that
is
easy
to
understand.
Generics
will
probably
hinder
us,
but
also
like
we've
all
been
there
like.
We
have
tried
to
write
clever
code,
but
could
you
understand
so
clever
code
afterwards?
A
This
is
also
magic
and
you
can
tell
magic
into
science
if
you
have
the
right
tools,
for
example,
if
something
like
Iago
expand
will
give
you
the
output
of
what
rust,
C
will
actually
compile
if
it
has
expanded
audio
macros.
So,
for
example,
this
struct
of
example
I
gave
you
can
paste
it
into
your
editor,
run
cago,
expand
and
see
how
it
generates
clap
code
underneath
codes
that
you
didn't
have
to
write
but
codes
that
will
be
there
and
codes
that
you
can
read
if
you
want
to.
A
Sadly,
even
the
best
tools
will
only
move
this
complexity
towards
using
the
tool,
but
not
actually
resolvers.
So
I
was
wondering.
How
can
we
be
aware
of
this?
How
can
we
come
up
with
good
ideas
to
write,
even
more
declarative
and
even
more
expressive
and
even
more
useful
code?
And
the
next
question
obviously,
is:
how
can
we
teach
this?
How
can
we
get
a
community
to
the
point
we
are
using?
Iterators
is
so
complex
that
you
do
not
question
it.
A
How
can
we
use
macros
in
a
non-magical
sense
because
they're
our
macros,
like
Beck,
did
you?
Did
you
see?
I
used
the
Veck
macro?
Now
it
doesn't
really
matter
because
it's
very
expressive.
You
know
the
types
come
back
the
macros
contract
and
to
expect
it
to
generate
the
vector
this
just
works,
and
so
how
can
we
actually
teaches
us?
A
So
several
approaches?
The
first
one
is
through
interactive
learning.
Ya
know
it's
the
first.
This
is
an
ambitious
one,
applause
I'm.
So
sorry,
but
I
really
like
flippy
lands
and
I
know
there
was
a
lot
of
potential
for
writing
even
more
clip
islands,
for
example,
there
is
one
issue
that
happened
to
be
open
by
me
about
linting
for
loops
to
suggest
iterators,
for
example.
This
is
the
code
snippet
that
uses
any
could
have
been
a
for
loop
with
an
if
statement
and
the
return
Clippy
code
to
texas
and
tell
you
hey.
A
A
A
A
A
If
I
understood
correctly
performance
implications
of
using
declarative
programming
starts
yes,
okay.
Well,
it
depends
on
what
you're
doing.
Of
course,
for
example,
all
the
examples
I
showed
using
30
and
using
iterators
were
actually
faster
because
they
did
less
work.
But
writing
this
code
manually
in
an
imperative
style
would
be
very
tedious.
You
can,
of
course,
do
it
and
because,
on
an
assembly
level,
everything
is
imperative.
You
can
optimize
every
program
as
an
imperative
level
more
rigorously
than
you
would
be
able
to.
C
It
seems
to
me
that
sometimes
declarative
programming
would
almost
require
some
additional
magic.
For
example,
when
you
did
the
take
five
and
collect
you
know,
it's
sort
of
it
does
say
what
to
do
and
not
how
to
do
it.
But
it's
not
sort
of
obvious
what
the
resulting
performance
characteristics
might
be,
because
you
don't
know
how
it
accomplishes
that
task.
C
A
You
have
to
measure
it
to
be
very,
very
certain,
of
course,
but
it
happens
to
be
the
lazy
to
write
as
a
very
fast.
So
in
this
case
you
will
find
that
it's
actually
the
same
performance
is
writing
volume
and,
of
course,
in
the
end,
you
have
to
trust
it
or
you
have
to
measure
it.
I'd
always
err
on
the
side
of
measuring
it,
but
this
magic
you're
talking
about
to
make
this
fast,
is
called
rusty
and
we
always
already
using
it
so
yeah.