out-of-bounds reads/writes, use-after-free, null pointer dereference and data races
..Rust is built on LLVM and strives to resemble Clang from LLVM’s perspective, any LLVM performance improvements also help Rust.
In the long run, the richer information in Rust’s type system should also enable optimizations that are difficult or impossible for C/C++ code
$ rustup install nightly
$ rustup default nightly
$ rustup target add arm-linux-androideabi
$ rustup override set nightly
$ rustup show
Rust’s Package Manager, Build tool and test runner.
$ cargo new hello_world --bin
cargo new
$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
└── main.rs
1 directory, 2 files
Cargo.toml
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Matt Gathu <mattgathu@gmail.com>"]
src/main.rs
fn main() {
println!("Hello, world!");
}
cargo build
$ cargo build
Compiling hello_world v0.1.0 (file:../hello_world)
$ cargo run
Fresh hello_world v0.1.0 (file:../hello_world)
Running `target/hello_world`
Hello, world!
The Rust community’s crate registry.
~ 16,944 crates
struct Rectangle {
width: i32,
height: i32,
}
impl Rectangle {
fn new(width: i32, height: i32) -> Rectangle {
Rectangle { width, height}
}
fn area(&self) -> i32 {
self.width * self.height
}
}
fn main() {
let rect = Rectangle::new(30, 50);
println!("The area of rectangle is {}.", rect.area());
}
trait Shape {
fn area(&self) -> f32;
}
impl Shape for Rectangle {
fn area(&self) -> f32 {
(self.width * self.height) as f32
}
}
fn print_area<S: Shape+Debug>(shape: &S) {
println!("The area of {:?} is {}",
shape,
shape.area());
}
enum Option<T> {
Some(T),
None,
}
pub fn is_some(&self) -> bool {
match *self {
Some(_) => true,
None => false,
}
}
Memory is managed through a set of rules that the compiler checks at compile time.
fn as_str(data: &u32) -> &str {
// compute the string
let s = format!("{}", data);
// (this does not compile in Rust)
&s
}
// s goes out of scope and is freed here
A value cannot outlive its owner.
let mut data = vec![1, 2, 3];
// get an internal reference
let x = &data[0];
// (this does not compile in Rust)
data.push(4);
println!("{}", x);
A mutable value cannot be shared/aliased.
A mutable reference is aliased if there exists another live reference to one of its ancestors or descendants.