Rust - gwenn/hypomnemata GitHub Wiki
let introduces a local variable. Variables are immutable by default.
let hi = "hi";
let mut count = 0;let immutable_box = box 5;
// Hand over the box, changing the mutability
let mut mutable_box = immutable_box;
// Modify the contents of the box
*mutable_box = 4;Borrowed pointer (&) versus Owned pointer (box) versus Raw pointer (*T, unsafe).
Option<&T> or Option<Box<T>> to represent potentially null pointer or reference.
All values in Rust are stack allocated by default. Values can be boxed (allocated in the heap) by creating a Box. A box is a smart pointer to a heap allocated value of type T. When a box goes out of scope, its destructor is called, the inner object is destroyed, and the memory in the heap is freed.
Boxed values can be dereferenced using the * operator; this removes one layer of indirection.
Box, Vec, String, File, Process
// `a` is a pointer to a heap allocated integer
let a = box 5;
// Copy `a` into `b`, now both are pointers to the same heap allocated
// data, but now, `b` owns the heap allocated data
// `b` is now in charge of freeing the memory in the heap
let b = a;
// Error! `a` can no longer access the data, because it no longer owns the
// heap memory// This function takes ownership of the box
fn eat_box(boxed_int: Box<int>) {
println!("destroying box that contains {}", boxed_int);
}
// This function borrows the box
fn peep_inside_box(borrowed_box: &Box<int>) {
println!("This box contains {}", borrowed_box);
}A reference is a non-owning view of a value. A reference can be obtained with the & (address-of) operator. It can be dereferenced by using the * operator.
Reference is a borrowing mechanism.
When data is borrowed, it also freezes. Frozen data can't be modified via the original object, until all the references to it go out of scope.
Data can be immutably borrowed any number of times, but while immutably borrowed, the original data can't be mutably borrowed.
All references actually have a type signature of the form &'a T, where 'a is the lifetime of the referenced object.
&str is like const char*.
"foo" (&'static str).
String::from_str("foo") <-> .as_slice().
A String is a general purpose growable string.
A string slice &str is a silce of string, should be used as an immutable input.
Strings will coerce into &str with an &
Viewing a String as a &str is cheap, but converting the &str to a String involves allocating memory.
struct Foo{};
impl Foo {
fn bar(); // static
fn foo(&self); // const
fn foo(&mut self);
}
impl <Trait> for <Type> {}
alias
pub type sqlite_int64 = ::libc::c_longlong;Rust does not support variadic functions.
A crate is Rust's unit of independent compilation. Crates can depend on other crates.
Each crate contains a hierarchy of modules. This tree starts off with a single module, called the crate root. Within the crate root, we can declare other modules, which can contain other modules, as deeply as you'd like.
src/lib.rs:
mod english {
}
src/lib.rs:
mod english;
src/english.rs:
... or
src/english/mod.rs:
#[link(name = "sqlite3")]
extern "C" {
pub static mut sqlite3_version: *const ::libc::c_char;
pub fn sqlite3_libversion_number() -> ::libc::c_int;
pub fn sqlite3_close(arg1: *mut sqlite3) -> ::libc::c_int;
pub fn sqlite3_open_v2(filename: *const ::libc::c_char,
ppDb: *mut *mut sqlite3, flags: ::libc::c_int,
zVfs: *const ::libc::c_char) -> ::libc::c_int;
}