Rust Lang version "hello world" starting here

I would like to ask you, great teachers.
I am learning Rust Lang, the BASIC, C language is the origin of my programming, but after writing Rust Lang for a while, I have noticed something that I need to keep in mind. It is.

The more advanced you are, the more beginner-friendly your programming should be.
So I actually wrote the following code and solved it myself.,
rustc kept warning me, and it took me 8 hours to finally get to :handshake:.

fn main() {
      trait MyTrait<AT, T> {
        fn hello_world(_: T) -> AT; 
    }
    struct MyStruct(String, String); impl MyTrait<String, MyStruct> for MyStruct
    impl MyTrait<String, MyStruct> for MyStruct {
        fn hello_world(v: MyStruct) -> String {
            format!("Hello world, My name is {} {}", v.0, v.1)
        }
    }
}

Here is the code I ended up with and the version of rustc.

$ rustc -V
rustc 1.73.0 (cc66ad468 2023-10-03)

The code you ended up with.

fn main() {
    trait MyTrait<AT, T> {
        fn hello_world(_: T) -> AT; }
    }
    struct MyStruct(String, String); impl MyTrait<String, MyStruct> for MyStruct
    impl MyTrait<String, MyStruct> for MyStruct {
        fn hello_world(v: MyStruct) -> String {
            format!("Hello world, My name is {} {}", v.0, v.1)
        }
    }
    let my_instance = MyStruct("T".to_owned(), "Kumagai".to_owned());
    let hello_world_string = <MyStruct as MyTrait<String, MyStruct>>::hello_world(my_instance);
    println!("{hello_world_string}");
    assert_eq!(hello_world_string, "Hello world, My name is T Kumagai".to_string());
}
   Compiling my_rust_version_hello_world v0.1.0 (/Users/k22/Develop/Rust/perfect-rust/my_rust_version_hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 1.21s
     Running `target/debug/my_rust_version_hello_world
Hello world, My name is T Kumagai

So, I would like to ask if you could give me a possible starting point for an advanced developer to learn more about Rust Lang concepts.

Have you read the Rust book? Hello, World! - The Rust Programming Language

Have you read Google's Rust cource for it's Android engineers: Welcome to Comprehensive Rust 🦀 - Comprehensive Rust 🦀

I think an immediate next step for you is to use Cargo to build your Rust programs instead of using rustc: Hello, Cargo! - The Rust Programming Language

With a background in C this should be fairly easy sailing for you.

4 Likes

That last output already looked like cargo output, I think the rustc was just to show the version.

3 Likes

If you feel pretty confident with the starting material in Learn Rust - Rust Programming Language already, a good next step is Jon Gjengset's (aka jonhoo) "Crust of Rust" series over on YouTube. He goes into detail on specific features of Rust: why they work they way they do, how to use them, and how to figure out that you're not using them right. His other content is also excellent, but not everyone is into watching 5 hour streams. His book is also quite essential reading too.

Amos's blog https://fasterthanli.me/ is another excellent resource, but it's whatever weird thing they've gotten into recently in absurd detail; great for learning something, not necessarily for learning something you're going to use tomorrow.

2 Likes

I am crying reading this book.
I am crying because I have bought many Rust Programming Language books published in Japan at expensive prices, but none of them satisfied me.
I could not find a book that I could recommend to everyone else.
If you want to write hello world in C language, I recommend you to use man command because you can check it by man command.
Example)

man 3 printf
#include

int main() {
printf("Hello, World!\n");
return 0;
}

For example,

cargo new hello-world --from-lang=c,java

I thought it would be easier to get started if we could have a hello-world template that matches the background

I cannot make out where you are having difficulty. I don't understand what you mean by "hello-world template that matches the background"?

When you read The Book you find instructions for creating a new Rust project with Cargo: Hello, Cargo! - The Rust Programming Language which says:

$ cargo new hello_cargo
$ cd hello_cargo

and how you will then find a ready made "Hello World" program in the file src/main.rs. Which will look like this:

fn main() {
    println!("Hello, world!");
}

I don't see how much easier it could be to get started. What do you have in mind?

6 Likes

It sounds like you are proposing that there should be different hello world programs depending on which language someone is coming from. That isn't a good idea, since the purpose of a hello world program is to test that the basic infrastructure is working (that is, it's as much about checking whether you can run rustc and cargo as it is about teaching you Rust), and identifying minimal required boilerplate (in this case, just a main function). Other languages have their own idioms and requirements that wouldn't be needed in a Rust hello world, and adding them is adding unnecessary complication. For example, your C program has main() returning 0 because that is how C signals the exit code of the program, that isn't required in Rust (exit codes in Rust are passed by either calling std::process::exit() or returning something from main() that implements the Termination trait, most commonly you would implicitly return () from main()). As another example, Java requires declaring a class to contain its main() function and requires declaring it static, but Rust is fine with functions not attached to types and doesn't really have classes like Java.

I'm not sure exactly what your Rust learning journey has looked like so far (you mentioned getting some other books), but trying to make Rust look like another language isn't going to help much (indeed, it is mistake #6 of how not to learn Rust). You may be best working through the Rust book linked above (and since you mentioned being in Japan, there is a Japanese translation in case you haven't already found it).

7 Likes

Ah yes, that's what "matches the background" means here.

The "Hello world" program in Rust is so simple I would expect it to be understandable by anyone coming from pretty much any other language. Surely it is immediately obvious to a C or Java programmer?

2 Likes

I'm a Rust newcomer, coming from a very long background in java and other object oriented languages.

In the end, the key point I have from everything is that, if anything, a person coming from these languages must be humble: there is no simple translation between java and rust. Period.

Even trivial things like having a Map that contains a dynamic type is not something that comes "out of the box" with Rust.

For me, as a person accustomed to runtime enabled types, reflection, almost all solutions I think of in the "java way" crumbles to dust when facing the unforgiving borrowing model that is the Rust essence.

Trying to find parallels in other languages is bound to get frustration as a result.

5 Likes

Not specifically from Java, but the biggest breakthrough I had with trying to get rust to do a thing you can do in these more dynamic languages was simply to realize that Rust simply provides more syntax to mean more ways to do the same thing: and that the shortest is the least flexible, highest performance one.

Keep adding dyn, Box, Mutex, Arc until it works, and you'll probably find the implementation that your other language actually uses: for example in GC languages classes are actually pretty close to Arc<Mutex<>>, and Promise/Futures are all Box<dyn Future>. The difference in Rust is you don't need to pay the runtime cost of these all the time, with the trade-off of the syntactic cost when you do.

6 Likes

At the same time, I've just watched yesterday the video where the primeagen comment on the article "Rust is a bad async language": https://www.youtube.com/watch?v=ANu2TDkKotw . One of the points is that, if you program stuff in the "java way" inside Rust, you end up with solutions that would be just better if there simply was a garbage collector running from the start. All the hassles and no benefit from a fine tuned garbage collector garbage-collected languages have.

But I'm new to the language, can't say how accurate that statement is.

1 Like

Nothing personal, but everytime that someone that comes from language X and tries to apply things from it into language Y and, when facing issues, all they have to say is "Language Y sucks", all I can think of is this:

4 Likes

And that is the reason I say that it is better not come with the preconceptions of another languages when learning rust.

I once found a person saying he was "fluent in java, it is not unlike turbo pascal" . And then the code written was a single function in the main() code of a single class... He was programming in "java"... all right.

And I did the same when I've decided to write with an oracle database a "object oriented" stored procedure (back in , what , 2009?). It "worked", sure. In the same way the program the turbo pascal guy wrote "worked". But it would be much better to write that procedure in another way, which would generate cleaner code and better maintainability.

1 Like

I would go as far as saying that for pretty much any language. Languages are tools, and as such they have use cases where they are best fitted than other similar tools.

2 Likes

And of course we must remember that just one tool is not the best for everything.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.