I think the issue is that the fields rho and p of your Grid structure are of type Matrix, so the views get converted to that type upon construction. This means a new copy is made, and therefore mutating it will not change prim. It works on the REPL because there you do not enforce a type on rho and p, and so they are truly views. If you really want to have rho and p as views of prim on Grid, they should be of SubArray type (with possibly a bunch of parametric arguments).
But alternatively, you may want to have only prim as a field in Grid, and have getters, like
rho(g::Grid) = view(g.grid,:,:,1) that will behave as you want…