Upload
lars-gregori
View
159
Download
5
Embed Size (px)
Citation preview
Motivation
Functional IoT (http://fpiot.metasepi.org/):
Imagine IoT devices are:
• connected to the internet
• developed in a short time
• storing personal data
• secure
• more intelligence
• inexpensive
Can C design IoT devices like that? No, can't.
rust-lang.org
Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.
Rust FAQ
• safe, concurrent, practical system language
• “We do not intend to be 100% static, 100% safe, 100% reflective, or too dogmatic in any other sense.”
• Mozilla Servo
Rust FAQ
• multi-paradigm
• OO
• no garbage collection
• strong influences from the world of functional programming
• built on LLVM
Rust FAQ
• Cross-Platform
• Android, iOS, …
• Conditional compilation #[cfg(target_os="macos")]
Rust Concepts
• ownership, the key concept
• borrowing, and their associated feature ‘references’
• lifetimes, an advanced concept of borrowing
‘fighting with the borrow checker’
https://doc.rust-lang.org/book/
Rust Concepts: Ownership
let v = vec![1, 2, 3];
let v2 = v;
// …
println!("v[0] is: {}", v[0]); ERRORuse of moved value: `v`
Rust Concepts: Ownership
let v = vec![1, 2, 3];
let v2 = v;
take_something(v2);
println!("v[0] is: {}", v[0]); ERRORuse of moved value: `v`
Rust Concepts: Ownership
fn foo(v1: Vec<i32>, v2: Vec<i32>) -> (Vec<i32>, Vec<i32>, i32) {
// do stuff with v1 and v2
(v1, v2, 42) // hand back ownership, and the result of our function
}
let v1 = vec![1, 2, 3];
let v2 = vec![1, 2, 3];
let (v1, v2, answer) = foo(v1, v2);
Rust Concepts: References and Borrowing
fn foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 {
// do stuff with v1 and v2
42 // return the answer
}
let v1 = vec![1, 2, 3];
let v2 = vec![1, 2, 3];
let answer = foo(&v1, &v2);
// we can use v1 and v2 here!
Rust Concepts: References and Borrowing
fn foo(v: &Vec<i32>) {
v.push(5);
}
let v = vec![];
foo(&v);
Rust Concepts: References and Borrowing
fn foo(v: &Vec<i32>) {
v.push(5);
}
let v = vec![];
foo(&v);
ERRORcannot borrow immutable borrowed content `*v` as mutable
Rust Concepts: References and Borrowing
let mut x = 5;
{
let y = &mut x;
*y += 1;
}
println!("{}", x);
RUN
Rust Concepts: References and Borrowing
let mut x = 5;
{
let y = &mut x;
*y += 1;
}
println!("{}", x); OK: 6
RUN
Rust Concepts: References and Borrowing
let mut x = 5;
{
let y = &mut x;
*y += 1;
}
println!("{}", x); OK: 6
☜
☜
☟
RUN
Rust Concepts: References and Borrowing
The Rules for borrowing:
1. Any borrow must last for a scope no greater than that of the owner.
2. Only one of these kinds of borrows, but not both at the same time:
• one or more references (&T) to a resource
• exactly one mutable reference (&mut T)
Rust Concepts: References and Borrowing
let mut x = 5;
let y = &mut x;
*y += 1;
println!("{}", x); Error
RUN
cannot borrow `x` as immutable because it is also borrowed as mutable
zinc.rs
Zinc is an experimental attempt to write an ARM stack that would be similar to CMSIS or mbed in capabilities but would show rust’s best safety features applied to embedded development.
Zinc is mostly assembly-free and completely C-free at the moment.
Rustup
https://www.rustup.rs/
rustup is an installer for the systems programming language Rust
> rustup install nightly-2016-09-17
> rustup override set nightly-2016-09-17
GNU ARM Embedded Toolchain
https://launchpad.net/gcc-arm-embedded/+download
arm-none-eabi-gcc
arm-none-eabi-gdb
…
eabi = Embedded Application Binary Interface (>)
zinc.rs Issue #336
Failing to build example. #336
src/hal/layout_common.ld
comment out *(.debug_gdb_scripts)
Debugging Rust
>openocd…-cgdb_port3333
>arm-none-eabi-gdb
(gdb)targetremote:3333
(gdb)file./target/thumbv7em-none-eabi/debug/blink_stm32f4
(gdb)breakmain
(gdb)continue
(gdb)next
What we’ve seen
• Rust Overview ✔
• Embedded Rust ✔
• Cross Compiling ✔
• MCU Debugging ✔
• Hello World