A safe, concurrent, practical language
Modules | Collections | Error Handling |
---|---|---|
Generics | Closures | Pointers |
Build System | Concurrency | Pointers |
fn main() {
println!("Hello, world!");
}
fn main() {
}
println!("Hello, world!");
Everything variable in rust is immutable by default
fn main() {
let x = String::from("hi");
println!("x is {}", x);
let y = x;
println!("x is {}", x);
}
fn main() {
let mut x = String::from("hello");
println!("x is {}", x);
concat_world(x);
println!("x is {}", x);
}
fn concat_world(mut x : String){
x.push_str("world");
}
fn main() {
let mut x = String::from("hello");
println!("x is {}", x);
let (isHello, x) = is_string_hello(x);
println!("x is {} and isHello is {}", x, isHello);
}
fn is_string_hello(mut x : String) -> (bool, String) {
return (x.eq("hello"), x);
}
fn main() {
let mut x = String::from("hello");
println!("x is {}", x);
let isHello = is_string_hello(&x);
println!("x is {} and isHello is {}", x, isHello);
}
fn is_string_hello(x : &String) -> bool {
return x.eq("hello");
}
fn main() {
let mut x = String::from("hello");
concat_hello(&x);
println!("x is {}", x);
}
fn concat_hello(x : &String){
return x.push_str("hello");
}
fn main() {
let mut x = String::from("hello");
concat_hello(&mut x);
println!("x is {}", x);
}
fn concat_hello(x : &mut String){
return x.push_str("hello");
}
int *c = malloc(sizeof(int));
free(c);
*c = 3; //writing to freed location!
{
let r;
{
let x = 5;
r = &x;
}
println!("r: {}", r);
}
{
let r; // -------+-- 'a
// |
{ // |
let x = 5; // -+-----+-- 'b
r = &x; // | |
} // -+ |
// |
println!("r: {}", r); // |
// |
// -------+
}
Ownership : To free memory
Lifetimes : To prevent dangling pointers