-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearnXinYminutes.rs
84 lines (61 loc) · 2.05 KB
/
learnXinYminutes.rs
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
// This is a comment. Line comment look like this...
// and extend multiple lines lik this.
/* Block Comments
/* can be nested. */ */
/// Documentation comments look like this and support markdwon notation.
/// # Examples
///
/// ```
/// let five = 5
/// ```
// 1. Basics//
#[allow(dead_code)]
// Functions
//`i32` is the type for 32-bit signed integers
fn add2(x: i32, y: i32) -> i32{
//Implicit return (no semicolon)
x+y
}
#[allow(unused_variables)]
#[allow(unused_assignments)]
#[allow(dead_code)]
fn main() {
// Numbers //
// Immutable Bindings //
let x: i32 = 1;
// Integer/Float Suffixes //
let y: i32 = 13i32;
let f: f64 = 1.3f64;
// Type Inference
// Most of the time, the Rust compiler can infer what type of variable is, so
// you don't have to write an explicit type annotation
// Throughout this tutorial, types are explicitly annotated in many places,
// but only for demonstrative purposes. Type inference can handle this for
// you most of the time.
let implicit_x = 1;
let implicit_f = 1.3;
// Arithmetic
let sum = x + y+ 13;
// Mutable Variable
let mut mutable = 1;
mutable = 4;
mutable += 2;
// Strings //
// String Literals //
let x: &str = "Hello Rusty";
// Printing
println!("{} {}", f, x);// 1.3 Hello Rusty
// A `String` - a heap-allocated String
// Stored as a `Vec<u8>` and always holds a valid UTF-8 Sequence.
// which is not null terminated.
let s: String = "hello world!".to_string();
// A string slice - an immutable view into another string
// This is a reference to a string
// doesn't actually contain the contents of the string, just a pointer to
// the beginning and a lenght of a string buffer.
// Statically allocated or contained in another object (in this case `s`).
// The string slice is like a view `&[u8]` into a `Vec<T>` .
let s_slice: &str = &s;
// declaring the variable s_slice of type &str string literal
// and assigning it to the reference of String s.
}