- 主题:我认为rust就是过100年也取代不了c++,证据如下
//////C++ 版本
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class Person {
public:
Person(std::string name, int age) : name(std::move(name)), age(age) {}
void greet() const {
std::cout << "Hello, I'm " << name << " and I'm " << age << " years old.\n";
}
private:
std::string name;
int age;
};
int main() {
std::vector<Person> people = {
{"Alice", 25},
{"Bob", 30},
{"Charlie", 35}
};
for (const auto& person : people) {
person.greet();
}
return 0;
}
/////Rust 版本
use std::fmt;
struct Person {
name: String,
age: u32,
}
impl Person {
fn new<T: Into<String>>(name: T, age: u32) -> Self {
Person {
name: name.into(),
age: age,
}
}
}
impl fmt::Display for Person {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Hello, I'm {} and I'm {} years old.", self.name, self.age)
}
}
trait Greeter {
fn greet(&self);
}
impl Greeter for Person {
fn greet(&self) {
println!("{}", self);
}
}
fn main() {
let people: Vec<Person> = vec![
Person::new("Alice", 25),
Person::new("Bob", 30),
Person::new("Charlie", 35),
];
for person in people.iter() {
person.greet();
}
}
--
FROM 114.254.51.*
Rust代码长,工资高,Rust赢
--
FROM 218.108.214.*
Rust可以写得比C++短啊。
cat src/main.rs
struct Person {
name: String,
age: u32,
}
impl Person {
fn greet(&self) {
println!("Hello, I'm {} and I'm {} years old.", self.name, self.age);
}
}
fn main() {
let people = vec![
Person { name: "Alice".into(), age: 25 },
Person { name: "Bob".into(), age: 30 },
Person { name: "Charlie".into(), age: 35 },
];
for person in &people {
person.greet();
}
}cargo run
Compiling bc v0.1.0 (rs\bc)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.09s
Running `target\debug\bc.exe`
Hello, I'm Alice and I'm 25 years old.
Hello, I'm Bob and I'm 30 years old.
Hello, I'm Charlie and I'm 35 years old.
--
FROM 171.213.181.*
chatgpt重构的,其实是差不多的。提示词说要短,它居然把成员的可读性干掉了。
struct Person(String, u32);
impl Person {
fn greet(&self) {
println!("Hello, I'm {} and I'm {} years old.", self.0, self.1);
}
}
fn main() {
for p in [Person("Alice".into(), 25), Person("Bob".into(), 30), Person("Charlie".into(), 35)] {
p.greet();
}
}
#include <iostream>
struct Person {
std::string n; int a;
void g() const { std::cout << "Hello, I'm " << n << " and I'm " << a << " years old.\n"; }
};
int main() {
for (auto [n,a] : {std::pair{"Alice",25}, {"Bob",30}, {"Charlie",35}})
Person{n,a}.g();
}
--
修改:z16166 FROM 221.220.174.*
FROM 221.220.174.*
Rust用了宏之后,数据初始化可以省很多不必要的部件,只填字符串、数字和逗号就行了。
C++23也能这样初始化,但需要一个可变参数模版,我在编程技术版贴过一个,要比Rust的宏多写几行。
struct Person(String, u32);
impl Person {
fn greet(&self) {
println!("Hello, I'm {} and I'm {} years old.", self.0, self.1);
}
}
macro_rules! folks {
($($name:expr, $age:expr),* $(,)?) => {
vec![$(Person($name.to_string(), $age)),*]
};
}
fn main() {
let people = folks![
"Alice", 25,
"Bob", 30,
"Charlie", 35,
];
for person in &people {
person.greet();
}
}
--
FROM 171.213.181.*
现在新语言学不动了,也不想学了
--
FROM 183.195.0.*
用AI coding,工程效率急剧提高。除少量代码外,AI的能力和效率都高于大部分程序员。
还在争语言有啥意义。
【 在 aiworking 的大作中提到: 】
: //////C++ 版本
: #include <iostream>
: #include <vector>
: ...................
--
FROM 114.249.63.*
没有ai支持的语言会死的透透的吧
【 在 Windsor 的大作中提到: 】
: 用AI coding,工程效率急剧提高。除少量代码外,AI的能力和效率都高于大部分程序员。
: 还在争语言有啥意义。
:
--
FROM 114.249.16.*
哎,我现在代码库里四五种语言,而且有继续变多的趋势,语言种类膨胀也是个问题。
【 在 Windsor 的大作中提到: 】
: 用AI coding,工程效率急剧提高。除少量代码外,AI的能力和效率都高于大部分程序员。
: 还在争语言有啥意义。
:
--
修改:cn62 FROM 36.250.215.*
FROM 36.250.215.*
C++自己过100年也取代不了C?
【 在 aiworking 的大作中提到: 】
: //////C++ 版本
: #include <iostream>
: #include <vector>
: ...................
--
FROM 114.253.34.*