初始化 Rust 项目,添加基本功能和依赖

This commit is contained in:
rsgl 2024-11-10 20:39:10 +08:00
commit 7da5c9392e
4 changed files with 314 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

140
Cargo.lock generated Normal file
View File

@ -0,0 +1,140 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.162"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
[[package]]
name = "ppv-lite86"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "rust-test"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "syn"
version = "2.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "zerocopy"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"byteorder",
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "rust-test"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"
[[bin]]
name = "rust-test"
path = "main.rs"

162
main.rs Normal file
View File

@ -0,0 +1,162 @@
fn main() {
let guess: u32 = "42".parse().expect("Not a number!");
println!("You guessed: {}", guess);
let heart_eyed_cat = '😻';
println!("{}", heart_eyed_cat);
let cc = another_function(3, 'a');
println!("The value of cc is: {}", cc);
// Statements and Expressions
let x = {
let y = 5;
y + 1
};
println!("The value of x is: {}", x);
for number in 1..4 {
println!("{}!", number);
}
let result = fibonacci_function(3);
println!("The result is: {}", result);
// 相互转换摄氏与华氏温度
let celsius = 30.0;
let fahrenheit = celsius_to_fahrenheit(celsius);
println!("{} celsius is {} fahrenheit.", celsius, fahrenheit);
let fahrenheit = 86.0;
let celsius = fahrenheit_to_celsius(fahrenheit);
println!("{} fahrenheit is {} celsius.", fahrenheit, celsius);
let mut s = String::from("hello");
s.push_str(", world!");
println!("{}", s);
let s2 = s;
println!("{}", s2);
// println!("{}", s);
let t = String::from("hello");
takes_ownership(t);
// println!("{}", t);
let y = 5;
makes_copy(y);
println!("{}", y);
let s4 = String::from("hello");
let len = calculate_length(&s4);
println!("The length of '{}' is {}.", s4, len);
// let s5 = String::from("hello");
// change(&s5);
let mn = String::from("hello world");
let word = first_word(&mn);
// mn.clear();
println!("The first word is: {}", word);
// let width1 = 30;
// let height1 = 50;
let rect1 = Rectangle {
width: 30,
height: 50,
};
dbg!(&rect1);
// println!("rect1 is {:#?}", rect1);
// println!(
// "The area of the rectangle is {} square pixels.",
// area(&rect1)
// );
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
let rect2 = Rectangle::square(3);
println!(
"The area of the rectangle is {} square pixels.",
rect2.area()
);
}
fn another_function(a: i32, char: char) -> i32 {
println!("Another function.");
println!("The value of a is: {}", a);
println!("The value of char is: {}", char);
5
}
fn fibonacci_function(n: i32) -> i32 {
if n == 0 {
return 0;
} else if n == 1 {
return 1;
} else {
return fibonacci_function(n - 1) + fibonacci_function(n - 2);
}
}
fn celsius_to_fahrenheit(celsius: f64) -> f64 {
celsius * 1.8 + 32.0
}
fn fahrenheit_to_celsius(fahrenheit: f64) -> f64 {
(fahrenheit - 32.0) / 1.8
}
fn takes_ownership(some_string: String) {
// some_string 进入作用域
println!("{some_string}");
} // 这里some_string 移出作用域并调用 `drop` 方法。
// 占用的内存被释放
fn makes_copy(some_integer: i32) {
// some_integer 进入作用域
println!("{some_integer}");
} // 这里some_integer 移出作用域。没有特殊之处
fn calculate_length(s: &String) -> usize {
s.len()
}
// fn change(some_string: &String) {
// some_string.push_str(", world");
// }
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[..i];
}
}
&s[..]
}
// fn area(width: u32, height: u32) -> u32 {
// width * height
// }
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
impl Rectangle {
fn square(size: u32) -> Rectangle {
Rectangle {
width: size,
height: size,
}
}
}