Rust

Quality & Efficiency
Combined

Rust logo

by Sam Jaques @

SW today

Intro to Rust

Rust's Pillars

Conclusion

SW today

Intro to Rust

Rust's Pillars

Conclusion

SW Development today

SW Development today

Programming Issues

## C++ Compile error hints 😩 ``` ‘from std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tee<_Key, _Val, _KeyOfValue, _Compare, _Alloc>:: _M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Key = SomeType; _Val = std::pair; _KeyOfValue = std::_Select1st >; _Compare = std::less; _Alloc = std::allocator >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree, std::_Select1st >,std::less, std::allocator > >::iterator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree, std::_Select1st >, std::less, std::allocator > >::const_iterator]’ ```

Quality Issues

Looks familiar?

Performance issue

Memory Issues

Concurrency Issues

SW today

Intro to Rust

Rust's Pillars

Conclusion

SW today

Intro to Rust

Rust's Pillars

Conclusion

Rust, what?

Influences

⬇

Rust

Source

Rust in production

Source

SW Development today

SW Development today

Safety vs control

SW today

Intro to Rust

Rust's Pillars

Conclusion

SW today

Intro to Rust

Rust's Pillars

Conclusion

SW today

Intro to Rust

Reliability

Performance

Productivity

Conclusion

Reliability

⬇

Guaranteed @ compile time

## How it should not be ```python animals = ["cat", "dog", "mouse"]; print("{}", animals[3]) ``` Runtime error: ``` Traceback (most recent call last): File "test.py", line 1, in IndexError: list index out of range ```
## How it should be ```rust let animals = ["cat", "dog", "mouse"]; println!("{}", animals[3]) ``` Compile time error 👊 ```sh # cargo Build error: this operation will panic at runtime --> no_segfaults.rs:2:15 | 2 | println!("{}", animals[3]) | ^^^^^^^^^^ index out of bounds: the length is 3 but the index is 3 ```

Rust's "secret sauce" 🛸

## Enforcements **Allow something, people use it** (_18 ways of initializing a variable in C++11_ 😮) `+`
**Don't enforce, people make mistakes** _even the best 🐎_

Enforcements

SW today

Intro to Rust

Reliability

Performance

Productivity

Conclusion

SW today

Intro to Rust

Reliability

Performance

Productivity

Conclusion

Performance comparison


How Rust deals with...

Performance

Design Principles


				fn sum_of_squares(input: &[i32]) -> i32 {
				    input.iter()
				         .map(|i| i * i)
				         .sum()
				}
				

All Greek to you? 😅


				fn sum_of_squares(input: &[i32]) -> i32 {
				    input.iter()
				         .map(|i| i * i)
				         .sum()
				}
				

Zero-cost


				use rayon::prelude::*;

				fn sum_of_squares(input: &[i32]) -> i32 {
				    input.par_iter()
				         .map(|i| i * i)
				         .sum()
				}
				

Parallalization

Fearless Concurrency

Fearless Concurrency

Safe message passing

Do not communicate by sharing memory; instead, share memory by communicating

				fn main() {
					std::thread::spawn(|| {
						let val = String::from("hi");
					});
				}
				

Safe message passing


				use std::sync::mpsc;

				fn main() {
					let (tx, rx) = mpsc::channel();

					std::thread::spawn(|| {
						let value = String::from("hi");
					});
				}
				

Safe message passing


				use std::sync::mpsc;

				fn main() {
					let (tx, rx) = mpsc::channel();

					std::thread::spawn(move || {  // move tx to new thread
						let value = String::from("hi");
						tx.send(value).unwrap();
					});

					let received = rx.recv().unwrap();
					println!("Got: {}", received);
				}
				

Powerful async-await



				async fn announce(src: IpAddr, msg: &[u8]) -> Result<()> {
					let socket: tokio::net::UdpSocket = create_socket(src)?;

					loop {
						socket
							.send_to(msg, AES_MULTICAST_SOCK)
							.await
							.expect("Failed to send SAP packet");

						time::sleep(SAP_ANNOUNCE_PERIOD).await;
					}
				}
				

Powerful async-await



				async fn announce(src: IpAddr, msg: &[u8]) -> Result<()> {
					let socket: tokio::net::UdpSocket = create_socket(src)?;

					loop {
						socket
							.send_to(msg, AES_MULTICAST_SOCK)
							.await
							.expect("Failed to send SAP packet");

						time::sleep(SAP_ANNOUNCE_PERIOD).await;
					}
				}
				

SW today

Intro to Rust

Reliability

Performance

Productivity

Conclusion

SW today

Intro to Rust

Reliability

Performance

Productivity

Conclusion

Compilation

Load file at compile time


					let json = include_bytes!("config.json");
					let config = serde_json::from_str::<Config>(
							&String::from_utf8_lossy(json)
						);
				
## Cucumber BDD tests 🥒 ```gherkin Scenario: One stream gets published When an AES67 flow with IP 239.1.0.1 is started And language NL for AES67 IP 239.1.0.1 gets configured And a tokbox session becomes active Then the stream is published ```

Cucumber BDD tests 🥒


					#[given(expr = "a tokbox session is active")]
					async fn session_ongoing(w: &mut World) {
						let session_info = tokbox_helper::create_session().await;
						REDIS.publish_session_info(session_info.clone());

						w.session = Some(session_info);
					}
				


				#[then(regex = "the stream is (not |un)?published")]
				async fn stream_published(w: &mut World, published: String) {
					let published = published.is_empty();
					w.expected_streams = published as u8;

					check_stream_published(w, published).await
				}
				

Powerful pattern matching


					match x {
						1 => println!("one"),
						2 | 3 => println!("two or three"),
						4..=9 => println!("within range"),
						x if x > 10 => println!("more than ten: {}", x),
						x => println!("{}", x),  // OR next line
						_ => println!("default Case")
					}
				

Powerful pattern matching


					match x {
						1 => println!("one"),
						2 | 3 => println!("two or three"),
						4..=9 => println!("within range"),
						x if x > 10 => println!("more than ten: {}", x),
						x => println!("{}", x),  // OR next line
						_ => println!("default Case")
					}
				

Exhaustive!

Downsides?

TypeScript Syntax


					function map<T, U>(array: T[], trans: (item: T) => U): U[]
					{ ... }
				

Rust Syntax 🤯


					fn map<B, F>(self, f: F) -> Map<Self, F>
					where
						Self: Sized,
						F: FnMut(Self::Item) -> B,
					{ ... }
				

Learning curve

SW today

Intro to Rust

Reliability

Performance

Productivity

Conclusion

SW today

Intro to Rust

Rust's Pillars

Conclusion

SW today

Intro to Rust

Rust's Pillars

Conclusion

Why Rust for developers

➖ New framework (integration)
➖ Missing frameworks
➖ Steep learning curve

➕ Feel as a GENIUS
➕ Splendid memory managment
➕ Embrace correctness in post-modern world
➕ Let's crush web app js world with wasm
➕ Resemblance of C & C++ mindsets

Why Rust for stakeholders

➖ Lack of experienced engineers

➕ Industry proven quality
➕ Better runtime performance
➕ Low energy usage
➕ Shorter validation cycle
➕ More robust software!

This was...

Rust, Quality & Efficiency Combined


Thanks for listening!


Questions? thoughts?

email me @ sam.jaques@tmceurope.com