diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4ef7dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +# Generated by Cargo +# will have compiled files in the `$OUT_DIR` folder +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# Cargo.lock + +# For IntelliJ +.idea/ + +# For Visual Studio Code +.vscode/ + +# For Vim +*.swp +*.swo +*~ + +# For Emacs +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# For Sublime Text +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache + +# For Kate +.session +.sessions + +# For Firefox Developer Edition +devtools.chrome.extension.build/ + +# For Mac +.DS_Store + +# For Linux +*~ + +# For Windows +Thumbs.db +ehthumbs.db +Icon? +desktop.ini + +# For Cargo documentation +/doc/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..9824aa0 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rust_project" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..1fdb625 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_project" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/rust_project b/rust_project deleted file mode 160000 index 4145a85..0000000 --- a/rust_project +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4145a856dcf81c7509e40883f848ea432d8f52ac diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5977f7f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,45 @@ +use std::io; + +fn main() { + println!("欢迎来到增强版 Rust 程序!"); + + // 演示变量声明和类型d + let name = "Rustacean"; // Rustacean 是 Rust 开发者的昵称 + let age: u32 = 10; + + println!("你好,{}!你使用 Rust 已经 {} 年了。", name, age); + + // 演示数组和循环 + let numbers = [1, 2, 3, 4, 5]; + println!("\n数字列表:"); + for (index, &value) in numbers.iter().enumerate() { + println!("索引 {}: 值 {}", index, value); + } + + // 计算数组元素总和 + let sum: i32 = numbers.iter().sum(); + println!("\n数组元素总和: {}", sum); + + // 条件判断 + if sum > 10 { + println!("总和大于 10"); + } else { + println!("总和小于或等于 10"); + } + + // 与用户交互 + println!("\n请输入您的名字:"); + let mut input = String::new(); + + match io::stdin().read_line(&mut input) { + Ok(_) => { + let input = input.trim(); // 移除换行符 + if !input.is_empty() { + println!("很高兴认识您,{}!", input); + } else { + println!("您没有输入任何内容。"); + } + } + Err(error) => println!("读取输入时发生错误: {}", error), + } +}