-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathownership.rs
More file actions
87 lines (73 loc) · 2.59 KB
/
Copy pathownership.rs
File metadata and controls
87 lines (73 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
fn main() {
let s = String::from("hello world....");
println!("{}", calculate_length(&s));
// let res = get_first_word(&s);
// println!("{}", res);
fn calculate_length(s: &String) -> usize {
s.len()
}
// 返回字符串中的第一个单词的位置
#[test]
fn get_first_word_size(s: &String) -> usize {
let bytes = s.as_bytes();
for (i,&item) in bytes.iter().enumerate() {
if item == b' ' {
return i;
}
}
s.len()
}
// 可变引用
fn ref_mutable(){
let mut s1 = String::from("hello1");
calculate_length(&s1);
{
let s2 = &s1;
s2 = s2 + "123";
println!("{}",s2);
}
println!("{}",s1);
}
// 字符串slice
#[allow(dead_code)]
fn string_slice() {
let s = String::from("hello world sam");
// 包含end
let s1 = &s[0..=1];
// 不包含end 如果想要从第一个索引(0)开始,可以不写两个点号之前的值
let s2 = &s[0..1];
let s3 = &s[..1];
println!("string slice s1 is:{}\ns2 is :{}\n", s1, s2);
println!("s3 is:{}\n", s3);
}
// 返回字符串中的第一个单词
fn get_first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i,&item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[..=i];
}
}
&s[..]
}
/*
## 所有权
* 所有权的规则有以下几点:
* Rust 中的每一个值都有一个被称为其 所有者(owner)的变量。
* 值有且只有一个所有者。
* 当所有者(变量)离开作用域,这个值将被丢弃。
* 变量的所有权总是遵循相同的模式:将值赋给另一个变量时移动它。当持有堆中数据值的变量离开作用域时,其值将通过 drop 被清理掉,除非数据被移动为另一个变量所有。
* 不能在拥有不可变引用的同时拥有可变引用。
## 引用和借用
* 可以使用大括号来创建一个新的作用域,以允许拥有多个可变引用,只是不能 同时 拥有
* 不能在拥有不可变引用的同时拥有可变引用
* “字符串 slice” 的类型声明写作 `&str`,它是一个`不可变引用`
### 总结
* 在任意给定时间,要么 只能有一个可变引用,要么 只能有多个不可变引用。
* 引用必须总是有效。
### 悬垂指针
* 所谓悬垂指针是其指向的内存可能已经被分配给其它持有者
## 一些概念
* 两次释放(相同)内存会导致内存污染,它可能会导致潜在的安全漏洞。
* 像整型这样的在编译时已知大小的类型被整个存储在栈上,所以拷贝其实际的值是快速的
*/