Szecsődi Imre
int main() {
int i = 32;
char (*pchar)[10] = new char[dim][10];
delete [] pchar;
}
fn main()
{
let name = "Thomas";
name = "Jane";
println!("Name is: {}", name);
}
error[E0384]: cannot assign twice to immutable variable
fn main()
{
let mut name = "Thomas";
name = "Jane";
println!("Name is: {}", name);
}
fn main()
{
let mut name = "Thomas";
name = "Jane";
let name_ref = &name;
println!("Name is: {}", name);
}
fn main()
{
let name = "Thomas";
let name_ref = &mut name;
}
error[E0596]: cannot borrow immutable local variable name as mutable
#[derive(Debug)]
struct Player
{
name: String,
age: u8,
description: String
}
impl Drop for Player{
// Destruktor függvény megadása
fn drop(&mut self) {
println!("Destrukor lefutott {:?}", self);
}
}
impl Player{
fn get_older(&mut self) {
age+=1;
}
}
fn main()
{
let mut our_player = Player
{
name: "Jones".to_string(), //converts &str (static string) to a String (heap memory)
age: 25,
description: "Just a happy guy.".to_string()
};
}
let s1 = String::from("hello");
let s2 = s1;
let s1 = String::from("hello");
let s2 = s1;
let s1 = String::from("hello");
let s2 = s1;
let s1 = String::from("hello");
let s2 = s1;
let s1 = String::from("hello");
let s2 = s1;
let s1 = String::from("hello");
let s2 = s1;
fn mover(moved: Player) -> Player
{
println!("I am {}, I've been moved into mover", moved.name);
moved
}
fn main()
{
let mut our_player = Player
{
name: "Jones".to_string(),
age: 25,
description: "Just a happy guy.".to_string()
};
mover(our_player);
println!("My name is {}, and I am being used after a move", our_player.name);
}
error[E0382]: use of moved value: our_player.name
fn immutable_borrow(borrowed: &Player)
{
println!("I am {}, I've been immutably borrowed", borrowed.name);
}
fn main()
{
let mut our_player = Player
{
name: "Jones".to_string(),
age: 25,
description: "Just a happy guy.".to_string()
};
immutable_borrow(&our_player);
println!("My name is {}, and I am being used after an immutable borrow", our_player.name);
}
fn main()
{
let age: u8 = 55;
mover(age);
println!("The age is: {}", age);
}
fn mover(age: u8) -> u8
{
println!("Age {} has been moved into mover!", age);
age
}
let x = 5;
let y = x;
#[derive(Debug, Clone)]
struct User {
a: String,
b: usize,
}
fn mover_function_with_return(mut user: User) -> User {
user.b = 456;
user
}
fn main()
{
let b = User{ a:String::from("Test2"), b: 234566 };
mover_function_with_return(b);
}
fn immutable_borrow(borrowed: &Player)
{
println!("I am {}, I've been immutably borrowed", borrowed.name);
}
immutable_borrow(&our_player);
fn main()
{
immutable_borrow(&our_player);
change_name(&mut our_player);
}
fn immutable_borrow(borrowed: &Player)
{
println!("I am {}, I've been immutably borrowed", borrowed.name);
}
fn change_name(borrowed: &mut Player)
{
borrowed.name = "My New Name".to_string();
}
fn main()
{
let my_immutable_return = immutable_borrow(&our_player);
change_name(&mut our_player);
println!("My name is {}, and I am being used after an mutable borrow", our_player.name);
}
fn immutable_borrow(borrowed: &Player) -> &Player
{
println!("I am {}, I've been immutably borrowed", borrowed.name);
borrowed
}
fn change_name(borrowed: &mut Player)
{
borrowed.name = "My New Name".to_string();
}
fn main()
{
let my_immutable_return = immutable_borrow(&our_player);
change_name(&mut our_player);
println!("My name is {}, and I am being used after an mutable borrow", our_player.name);
}
fn immutable_borrow(borrowed: &Player) -> &Player
{
println!("I am {}, I've been immutably borrowed", borrowed.name);
borrowed
}
fn change_name(borrowed: &mut Player)
{
borrowed.name = "My New Name".to_string();
}
fn main()
{
{
let my_immutable_return = immutable_borrow(&our_player);
}
change_name(&mut our_player);
println!("My name is {}, and I am being used after an mutable borrow", our_player.name);
}
fn main() {
// Konstans változó
let a : i32 = 123;
// Nem konstans változó
let mut b : i32 = 987;
// Objektum létrehozás
let c = User{ a:String::from("Test2"), b: 234566 };
// Move
let d : User = c;
// Copy, ha implementálja a Copy-t a típus
let d1 : i32 = a
// Clone, ha implementálja a Clone-t a típus
let e : User = c.clone()
// Immutable Borrow
let f : i32 = &c.b;
// Mutable Borrow
let g : i32 = &mut c.b;
}
Rust Mutability, Moving and Borrowing - The Straight Dope - Cikk a Rust fórumon - https://users.rust-lang.org/t/rust-mutability-moving-and-borrowing-the-straight-dope/22166