#Im new, can i get a start?

76 messages · Page 1 of 1 (latest)

ripe sphinx
#

I am familiar with c(barely) c++(decent) c#(good) but i was wondering how many ways there are to use the print function, and what fn means(is it function and why is it Infront of a class)

#

in c# you could do Console.Writeline("")

#

and

#

Console.Writeline($"{}")

#

or

#

Console.Writeline (@" ")

livid sandal
#

usually, you use a macro (println! or print!)

ripe sphinx
azure warren
#

right

ripe sphinx
#

and println! is like with

viscid gazelle
#

what do you mean with "in front of a class". Do you mean the !?

ripe sphinx
#

fn main()

azure warren
#

i think its two different questions, one about printing and another about function syntax

azure warren
#

main is just the name of a function in this case (and main is where binaries start from, similar to c)

livid sandal
#

print! and println! are macros to allow a variable number of arguments, for formatting

ripe sphinx
#

in c its int Main() but idk why its fn in rust

viscid gazelle
#

The syntax is different

#

i might ask "why doesn't C use fn or fun or def to declare functions"

azure warren
#

just different syntax, c has ReturnType function_name(ArgType argname) and rust has fn function_name(argname: ArgType) -> ReturnType

livid sandal
#

yup

viscid gazelle
#

the main reason why syntax in rust is what it is is to make it easy to parse, both for compilers and humans

ripe sphinx
#

whats the #include/using in rust?

livid sandal
#

use ...;

azure warren
#

use crate::module::Type; or something similar

viscid gazelle
#

#include is an entirely different beast than using by the way

#

it does a lot more

#

there is no equivalent of it in rust

azure warren
#

overall the most important thing for anyone to do when picking up rust is to read the book, just writing bits of code and googling what you think you need is going to lead to torture

#

@analog wind -book

livid sandal
#

include! macro

#

but it's not for usual stuff

ripe sphinx
#

btw if i ever make a class file outside the main file, do i have to write use the namespace and the file aswell or only the namespace

viscid gazelle
#

#include can do things include! can't. mostly horrible things, but it can

livid sandal
azure warren
livid sandal
livid sandal
#

well, rust isn't OOP

ripe sphinx
livid sandal
#

there is a thing in rust, called traits, and imo it's actually better than normal inheritance stuff

ripe sphinx
#

im listening

viscid gazelle
ripe sphinx
#

what about polymorphism?

livid sandal
#

you do that with traits

viscid gazelle
#

we have traits, which are kinda like interfaces in C#

ripe sphinx
#

child : parent

#

like this?

ripe sphinx
livid sandal
# ripe sphinx child : parent

trait Trait2 : Trait1 {}
this syntax is used to define traits that can only be implemented for things that already have the trait on the right implemented

livid sandal
stiff kernel
#

It was already linked here, but I'd strongly recommend you read through the Book, from the start, instead of jumping around to all the unique features of Rust.

livid sandal
azure warren
#

rust doesn't have inheritance, but there's a few kinds of polymorphism

livid sandal
ripe sphinx
#

well can you at least tell me rust memory safety, i use pointers sometimes, and when i use references i have no idea if i accidently did it

azure warren
#

in rust, references are just pointers that have some compile-time terms and conditions attached

stiff kernel
#

It's also pretty hard to accidentally use pointers. You can create pointers pretty easily, but actually using them would require you to wrap it in the unsafe keyword.

azure warren
stiff kernel
#

Like I said, really suggest you read the Book (https://doc.rust-lang.org/book/), it'll give you detailed explanations (in a structured way) of these concepts, without piling all of them on at the same time.

livid sandal
#

and all "owned" values get destroyed when they get out of scope

ripe sphinx
livid sandal
#

then you use other stuff for that

azure warren
#

same principle, rust uses raii much like c++

#

if a collection/structure goes out of scope, anything in it that needs to be freed will be, and so on recursively

livid sandal
#

yes, usually there is no need for manually deallocating things

#

unless you deal with unsafe code, which is, yk, unsafe

ripe sphinx
#

neat

livid sandal
#

there's a lot of nice features in rust, but it takes some time to know about them, so just try to be patient and don't rush things