// use rand::Rng;
// use std::cmp::Ordering;
fn main() {
let config_max = Some(3u8);
if let Some(max) = config_max {
println!("The maximum is configured to be {}", max);
}
let m = 3u8;
println!("m is {}", m);
}
// fn main() {
// fn plus_one(x: Option<i32>) -> Option<i32> {
// match x {
// None => None,
// Some(i) => Some(i + 1),
// }
// }
// let five = Some(5);
// let six = plus_one(five);
// let none = plus_one(None);
// }
// #[derive(Debug)]
// enum UsState {
// Alabama,
// Alaska,
// }
// enum Coin {
// Penny,
// Nickel,
// Dime,
// Quarter(UsState),
// }
// fn value_in_cents(coin: Coin) -> u8 {
// match coin {
// Coin::Penny => 1,
// Coin::Nickel => 5,
// Coin::Dime => 10,
// Coin::Quarter(state) => {
// println!("quarter {:?}", state);
// // 打印数字需要将变量as转换成具体的类型
// println!("quarter {}", state as u8);
// 25
// }
// }
// }
// fn main() {
// value_in_cents(Coin::Quarter(UsState::Alaska));
// value_in_cents(Coin::Quarter(UsState::Alabama));
// }
// match 枚举
// enum Coin {
// Penny,
// Nickel,
// Dime,
// Quarter,
// }
// fn main() {
// let coin = Coin::Penny;
// let res = value_in_cents(coin);
// println!("res is {}", res);
// }
// fn value_in_cents(coin: Coin) -> u8 {
// match coin {
// Coin::Penny => {
// println!("lucky penny");
// 1
// }
// Coin::Nickel => 5,
// Coin::Dime => 10,
// Coin::Quarter => 25,
// }
// }
// 枚举使用
// #[derive(Debug)]
// enum IpAddrKind {
// V4,
// V6,
// }
// #[derive(Debug)]
// struct IpAddr {
// kind: IpAddrKind,
// address: String,
// }
// fn main() {
// let home = IpAddr {
// kind: IpAddrKind::V4,
// address: String::from("127.0.0.1"),
// };
// let loopback = IpAddr {
// kind: IpAddrKind::V6,
// address: String::from("::1"),
// };
// println!("{:?},{:?}", home, loopback);
// }
// println! 的使用
// {:?}、{:#?} 打印结构体需要在结构体加debug标识
// #[derive(Debug)]
// struct AlwaysEqual {
// username: u32,
// }
// fn main() {
// println!("Hello, world!");
// let ae = AlwaysEqual { username: 1 };
// println!("ae is {:?}", ae);
// println!("ae is {:#?}", ae);
// dbg!(&ae);
// dbg!(ae);
// }
// impl接口的使用
// 方法的定义
// struct AlwaysEqual {
// username: u32,
// }
// impl AlwaysEqual {
// fn print(&self) {
// println!("hello: self.username={}", self.username)
// }
// }
// fn main() {
// let subject = AlwaysEqual { username: 1 };
// subject.print()
// }
// struct User {
// username: String,
// email: String,
// active: bool,
// }
// fn main() {
// let email = String::from("1@gmail.com");
// let username = String::from("jack");
// let u = build_user(email, username);
// println!("u is {},e is {},a is {}", u.username, u.email, u.active);
// let user2 = User {
// email: String::from("another@example.com"),
// // 指定未显式设置的其余字段应与给定实例中的字段具有相同的值。
// ..u
// };
// println!(
// "u2 is {},e2 is {},a2 is {}",
// user2.username, user2.email, user2.active
// );
// }
// fn build_user(email: String, username: String) -> User {
// // field init简写
// User {
// active: true,
// username,
// email,
// }
// }
// slice 使用
// fn main() {
// let mut s = String::from("hello world");
// let word = first_word(&s); // word will get the value 5
// // [start..end] 从start开始到end结束,不包括end
// let word_one = &s[0..5];
// let word_two = &s[6..11];
// println!("{} {}", word_one, word_two);
// s.clear(); // this empties the String, making it equal to ""
// let word1 = first_word(&s); // word will get the value 5
// println!("the first word is: {},{}", word, word1);
// }
// fn first_word(s: &String) -> usize {
// let bytes = s.as_bytes();
// // iter() 返回集合中的每一个元素
// for (i, &item) in bytes.iter().enumerate() {
// if item == b' ' {
// return i;
// }
// }
// s.len()
// }
// for 使用
// fn main() {
// let arr = [1, 2, 3, 4, 5];
// for element in arr {
// println!("the value is: {}", element);
// }
// let tuple = (1, 2, 3);
// }
// while 使用
// fn main() {
// let mut number = 3;
// while number != 0 {
// println!("{}!", number);
// number = number - 1;
// }
// println!("over!!!");
// }
// 多层loop使用
// fn main() {
// let mut count = 0;
// 'counting_up: loop {
// println!("count = {count}");
// let mut remaining = 10;
// loop {
// println!("remaining = {remaining}");
// if remaining == 9 {
// break;
// }
// if count == 2 {
// break 'counting_up;
// }
// remaining -= 1;
// }
// count += 1;
// }
// println!("End count = {count}");
// }
// loop使用
// fn main() {
// let mut x = 0;
// loop {
// x = x + 1;
// if x == 4 {
// continue;
// }
// if x == 5 {
// break;
// }
// println!("x is {}", x);
// }
// }
// 流程控制
// fn main() {
// let number = 3;
// if number > 5 {
// println!("condition was true");
// } else {
// println!("condition was false");
// }
// if number % 4 == 0 {
// println!("number is divisible by 4");
// } else if number % 3 == 0 {
// println!("number is divisible by 3");
// } else if number % 2 == 0 {
// println!("number is divisible by 2");
// } else {
// println!("number is not divisible by 4, 3, or 2");
// }
// let 语句中使用 if
// let x = 2;
// let number = if x > 10 { 5 } else { 6 };
// println!("The value of number is: {number}");
// }
// 函数调用和入参、多参数返回
// fn main() {
// let y = {
// let x = 3;
// x + 1
// };
// let five_var = five(1);
// println!("The value of y is: {y},five_var is {five_var}")
// let (x, y) = multiple_params();
// println!("x is {x}, y is {y}");
// }
// 多返回参数
// fn multiple_params() -> (i32, i32) {
// return (1, 2);
// }
// fn five(x: i32) -> i32 {
// // return和不加都可以
// // return x + 51;
// x + 51
// }
// fn main() {
// another_fn(5, "hello");
// }
// &str 和 String 的区别是,前者是静态分配的,后者是动态分配的
// 推荐用snake_case 命名
// fn another_fn(x: i32, s: &str) {
// println!("another, {},{}", x, s);
// }
// 越界问题,rust会提示
// fn main() {
// let a = [1, 2, 3, 4, 5];
// println!("Please enter an array index.");
// let mut index = String::new();
// io::stdin()
// .read_line(&mut index)
// .expect("Failed to read line");
// let index: usize = index
// .trim()
// .parse()
// .expect("Index entered was not a number");
// let element = a[index];
// println!("The value of the element at index {index} is: {element}");
// }
// 数据类型
// Length Signed Unsigned
// 8-bit i8 u8
// 16-bit i16 u16
// 32-bit i32 u32
// 64-bit i64 u64
// 128-bit i128 u128
// arch isize usize
// fn main() {
// let guess: u32 = "1".parse().expect("Not a number!");
// println!("guess: {}", guess);
// let c = 'z';
// let z: char = 'ℤ'; // with explicit type annotation
// let heart_eyed_cat = '😻';
// let chinese = '中';
// 两种方式访问元组
// let tup: (i32, f64, u8) = (500, 6.4, 1);
// let (x, y, z) = tup; // 解构
// println!("{} {} {}", x, y, z);
// println!("{} {} {}", tup.0, tup.1, tup.2);
// let arr = [1, 2, 3, 4];
// let arr = [3; 5];
// println!("{} {} {}", arr[0], arr[1], arr[2]);
// }
// 重复使用let就是shadowing
// // 多次使用let关键字,每次都改变了变量的类型
// fn main() {
// let spaces = " ";
// let spaces = spaces.len();
// println!("The value of spaces is: {spaces}");
// }
// shadwoing 作用域
// fn main() {
// let x = 5;
// let x = x + 1;
// {
// let x = x * 2;
// println!("The value of x is: {x}");
// }
// println!("The value of x is: {x}");
// }
// 常量
// const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
// 可变变量
// fn main() {
// let mut x = 5;
// println!("The value of x is: {x}");
// x = 6;
// println!("The value of x is: {x}");
// println!("The value of THREE_HOURS_IN_SECONDS is: {THREE_HOURS_IN_SECONDS}");
// }
// 数字游戏
// fn main() {
// println!("Guess the number!");
// let secret_number = rand::thread_rng().gen_range(1..=100);
// println!("The secret number is: {secret_number}");
// loop {
// println!("Please input your guess.");
// let mut guess = String::new();
// io::stdin()
// .read_line(&mut guess)
// .expect("Failed to read line");
// let guess: u32 = guess.trim().parse().expect("Please type a number!");
// println!("You guessed: {guess}");
// match guess.cmp(&secret_number) {
// Ordering::Less => println!("Too small!"),
// Ordering::Greater => println!("Too big!"),
// Ordering::Equal => {
// println!("You win!");
// break;
// }
// }
// }
// }
Rust学习笔记
最新推荐文章于 2023-11-30 19:26:19 发布