use std::rc::Rc;
use std::sync::Arc;
struct Person {
name :String,
age:i32,
}
#[derive(Copy,Clone, Debug)]
struct TestData {
p1:i32
}
fn get_data(str1 :String) -> String{
str1
}
fn main() {
//所有权
let str1 = "hello";
{
let str2 = "world";
println!("{}",str2);
}
//println!("{}",str2)
println!("{}",str1);
//str的复制
let a1 = "123";
let b1 = a1;
println!("a1={}", a1);
//全新的实例
println!("b1={}", b1.to_string());
//转移的地方,1 函数返回 2 构建新值 3 给函数传值
let vec2:Vec<i32> = (0..5).collect();
println!("{:#?}", vec2);
let vec3 = vec2;
//error 构建新值
//println!("{:#?}", vec2);
println!("{:#?}", vec3);
let str3 :String = String::from("tom");
println!("str3={}", str3);
let age_number:i32 = 31;
let p1 = Person{name:str3, age:age_number};
println!("p1.name={} p1.age={} ", p1.name, p1.age);
//error 构建新值
//println!("str3={}", str3);
let str4 :String = String::from("tom");
println!("str4={}", str4);
let strt5 = get_data(str4);
println!("strt5={}", strt5);
//error 给函数传值
//println!("str4={}", str4);
let mut vec5 = Vec::new();
for i in 101..106 {
vec5.push(i)
}
let one_str = vec5[0];
println!("stone_strr4={}", one_str);
println!("{}", vec5.pop().unwrap());
println!("{}", vec5.swap_remove(1));
let axxx = std::mem::replace(&mut vec5[1], 122222);
println!("{:#?},{}", vec5, axxx);
//有意思的是值的获取必须是变量的,不能转移
let v3 = vec!{"a1".to_string(),"a2".to_string(),"a3".to_string()};
for mut s in v3 {
s.push('x');
println!("{}", s);
}
//不同的类型转移,不同的类型赋值
//大多数类型转义,以下类型或copy:整数,char,bool,元组,数组
//任何值在被清除后需要特殊处理的类型都不能是copy类型
let test_data1 = TestData{p1:123};
let test_data2 = test_data1;
println!("{:#?}", test_data1);
println!("{:#?}", test_data2);
println!("{:#?}", test_data2.p1);
//rc
let rc1:Rc<String> = Rc::new("aaaaa".to_string());
let rc2:Rc<String> = rc1.clone();
let rc3:Rc<String> = rc1.clone();
println!("{:#?}", rc1);
println!("{:#?}", rc2);
println!("{:#?}", rc3);
//arc 多线程版本的引用计数
let arc1:Arc<String> = Arc::new("aaaaa".to_string());
let arc2:Arc<String> = arc1.clone();
let arc3:Arc<String> = arc1.clone();
println!("{:#?}", arc1);
println!("{:#?}", arc2);
println!("{:#?}", arc3);
}
1
共 0 条评论, 1 页
评论区
写评论还没有评论