fn main(){ //str在这里无效,它尚未声明
let str = "生活很美好"; //从此处起,str开始有效
} //此作用域已经结束,str不再有效
let str = "hello"
和 let str = String::from("hello")
的区别
let str = "hello"
let str = String::from("hello")
fn main(){
let str = "hello";
let mut str1 = String::from("hello");
str1.push_str(",Tom");
println!("{}",str1);// 输出 hello,Tom
}
/*
当变量离开作用域,rust为我们调用一个特殊函数,这函数叫做drop自动释放内存
*/
fn main(){
let str = String::from("hello"); //从此起,str开始有效
//使用str
} //此作用域已结束,str不在有效
let x = 20;
let y = x;
let str = String::from("hello");
let str1 = str;
fn main() {
let str = String::from("自强不息");
let str1 = str;
// println!("{}", str);// error
}
str
和 str1
离开作用域,它们都会尝试释放相同的内存,两次释放相同内存会导致内存污染let str1 = str;
之后,Rust认为str
不再有效clone的通用函数
fn main() {
let str = String::from("自强不息");
let str1 = str.clone();
println!("str = {},str1 = {}", str, str1); // str = 自强不息,str1 = 自强不息
}
fn main() {
let str = String::from("自强不息"); //str进入作用域
takes_ownership(s); // str的值移动到函数里面,所以这里不再有效
let num = 20; // num进入作用域
makes_copy(20); // num移动函数里面,但i32是拷贝,所以在后面可继续使用x
} // num移除作用域,str的值已被移走
fn takes_ownership(some_string: String) {
// some_thing 进入作用域
println!("{}", some_string);
} // some_thing移除作用域并调用 drop 方法
fn makes_copy(some_interger: i32) {
// some_integer进入作用域
println!("{}", some_interger);
} // some_integer移除作用域
fn main() {
let str = String::from("自强不息");
let (str1, len) = calculate_length(str);
println!("The length of {} is {}", str1, len); // The length of 自强不息 is 12
}
fn calculate_length(str: String) -> (String, usize) {
let length = str.len();
(str, length)
}
fn main() {
let str = String::from("自强不息");
let len = calculate_length(&str);
println!("The length of {} is {}", str, len); // The length of 自强不息 is 12
}
fn calculate_length(str: &String) -> usize {
let length = str.len();
length
}
&str
语法让我们创建一个指向值str
的引用,但是并不拥有它,所以当引用停止使用
时,它所指向的值也不会被丢弃
创建一个引用的行为称为借用,只有使用权限,没有拥有权限,借用变量不可修改
可变引用
fn main() {
let mut str = String::from("天行健,君子以");
change(&mut str);
println!("{}", str)
}
fn change(some_thing: &mut String) {
some_thing.push_str("自强不息!");
}
fn main() {
let mut str = String::from("天行健,君子以");
let str1 = &mut str;
// let str2 = &mut str; //error
// println!("{},{}", str1, str2);
}
fn main(){
let mut str = String::from("自强不息");
{
let str1 = &mut str;
}
let str2 = &mut str;
}
str1
和str2
的作用域在println!
最后一次使用之后结束str3
创建和str1 & str2
作用域没重叠fn main(){
let mut str = String::from("自强不息");
let str1 = & str;
let str2 = & str;
println!("{},{}",str1,str2);// 自强不息,自强不息
let str3 = &mut str;
println!("{}",str3);//自强不息
}
垂悬指针
fn main(){
let reference_to_nothing = dangle();
}
/*
// error
fn dangle() -> &String{
let str = String::from("自强不息");
&str //返回字符串str的引用
}// 这里str离开作用域并被丢弃,其内存释放
*/
fn dangle() ->String{
let str = Stirng::from("自强不息");
str
}
fn main() {
let mut str = String::from("hello world");
let word = first_word(&str);
println!("{}", word); // 5
str.clear();
}
fn first_word(str: &String) -> usize {
let bytes = str.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
// b' ',表示字节字面量,它表示空格字符
if item == b' ' {
return i;
}
}
str.len()
}
String
中一部分值的引用let str = String::from("hello world");
let hello = &str[0..5];
let world = &str[6..11];
println!("{}",world);//world
fn main() {
let mut str = String::from("hello world");
let hello = first_word(&str[..]);
//str.clear();// clear需要清空String,尝试获取一个可变引用,而hello使用了不可变引用
println!("The value of is {}", hello);
}
fn first_word(str: &str) -> &str {
let bytes = str.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &str[0..i];
}
}
&str[..]
}
let str = "hello world";
fn first_world(st: &str) -> &str{}
let arr = [1,2,5,1,6,8];
let slice = &arr[1..3];
assert_eq!(slice,&[2,5]);