-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht5_strings.mojo
60 lines (42 loc) · 1.21 KB
/
t5_strings.mojo
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
# from String import String
from utils.vector import DynamicVector
def try_string():
# todo x: String 类型已经内置, 无需导入
# - ref:
# - https://docs.modular.com/mojo/lib.html
# - https://mojodojo.dev/guides/intro-to-mojo/basic-types.html#strings
#
s = String("Mojo🔥")
print(s)
# x = s.buffer
# x = 20
# print(x)
print(s[0])
print(ord(s[0]))
def try_vector():
var vec = DynamicVector[Int8](2)
vec.push_back(78)
vec.push_back(79)
# from Pointer import DTypePointer
# from DType import DType
# todo x: 转换为 String
# - 内置 StringRef, DTypePointer, DType, 不需要导入
let vec_str_ref = StringRef(DTypePointer[DType.int8](vec.data).address, vec.size)
print(vec_str_ref)
# print(vec)
vec[1] = 78
print(vec_str_ref)
# todo x: 深拷贝 deep copy
let vec_str = String(vec_str_ref)
print("vector str:", vec_str)
vec[0] = 65
vec[1] = 65
print(vec_str) # todo x: 并不会改变
var lit = "This is my StringLiteral"
print(lit)
emoji = String("🔥😀")
print("fire:", emoji[0:4])
print("smiley:", emoji[4:8])
def main():
try_string()
try_vector()